The project I have inherited has one jar file with no package name. I want to add a package name to it so I can refer to the JDialogs in my Demo app (JDemo). I am not in a position to change the directory structure of the code which is stored in a path like :
C:\Product\Version\Component\JavaApplet\src. I know the package name normally follows the directory hierarchy, but I'm not sure where one starts from (root) and I haven't been able to choose anything that is recognised.

C:\Product\Version\Component\JavaApplet\classes\myapplet.jar is added to the classpath.

I'm not concerned about prefixing the name with the reverse domain name as it will not be distributed. My only goal is to have a package name so I can use the JDialogs in JDemo. What is my "reverse engineered" package name?

The package name is contained in the compiled class file. Therefore you can't change the package name without modifying and recompiling the source code.

Sorry if my question was misleading. I want to put a package name in my Japplet descendant .java file. Given my dir structure is as given and fixed what package name can I give it?

So if I understand correctly this time, the directory C:\Product\Version\Component\JavaApplet\src is your 'source root' where your java files without package name are stored. Then you have to create a directory there, move your java file into that directory, and add this directory as the package name to your java file.

If you have your java filed stored as:

C:\Product\Version\Component\JavaApplet\src\abc\def\MyApplet.java

then you have to add this statement to MyApplet.java:

package abc.def;

Ok, thanks. All my .java files are in the src directory, so the JApplet descendant is in :

C:\Product\Version\Component\JavaApplet\src\MyApplet.java

From your example then that would suggest I cannot give this a name as there are no sub directeries of src?

So if you can't create any subdirectory below src, then there's only one ugly thing left: go up one directory and use 'src' as your package directory.

Oh, there's another thing: you can use a package prefix, i.e. you are telling IDEA that everything in 'src' should be in a package 'abc.def'. Compiled classes are then automatically placed in a corresponding subdirectory. Would that work for you?

Thanks. I'm not sure how to state my question so we are on the same page. All my .java code is in the src directory. Ideally | do not want to have to jump through hoops to get it moved. Without moving my code can I give my .jar file a package name? Yes or No Please, as that will help my understanding.

My compiled classes currently go into which is at the same level as ]]>. Ideally I do not want to have to move anything in source control. Any way of using this prefix solution but not having the output as a sub dir?

Then you're out of luck. A package name is not something artificial but is directly coupled to the directory structure of your classes. You won't be able to compile or launch a class if the directory structure doesn't match the package structure.

Currently all my code compiles and runs fine. My only reason for wanting to add a package name is that I believe it's the only way I can use my code when I create demos in JDemo - maybe I am wrong there.

My understanding from what you have written is that my packages are the class files and if they are included in the same level as the .java file, as they are now, then they can't be "redistributed", ie. given a package name.

I'm not familiar with JDemo, so I can't tell you if you're really forced to have a package name.

But for the second part: yes, you're right. If you can't move your class files into a subdirectory then you can't give them a package name.

Thanks a lot for that, good to know.

JDemo is not necessarily relevant. I just want to be able to package my code to reuse in another project - the solution is packages.

I have now moved the jar file to a subdirectory of the root where the source is in a ]]> sub directory.

I have set the package prefix to com.trilogycomms. I then try to add a package name com.trilogycomms.classes; It says this package name does not correspond to the file path com.trilogycomms

Any clues what I am doing wrong?

Message was edited by:
Tom Corcoran Thurs 25 Oct 17:50 GMT

I have set the package prefix to com.trilogycomms. I
then try to add package com.trilogycomms.classes; It
says this package name does nto correspond to the
file path com.trilogycomms

If you have a package prefix com.trilogycomms for your src directory and you create a class Test directly inside this directory, then the fully qualified name is com.trilogycomms.Test. If you want the fqn to be com.trilogycomms.classes.Test, then you have either to create a subdirectory classes (I know, you can't...) or you change your package prefix to com.trilogycomms.classes.

Thanks. Ok. I am finding the intellij help very weak on packages and prefixes. My only goal here is to have my classes accessible in another project. So am I correct to say that with my source in and my jar in ]]> at the same level it is not possible to have a package name for this jar?

The location of your jar file doesn't matter at all. It's not the jar file which has a package name, but each class contained in the jar file may reside inside a package.

But once again: the package isn't just an artificial name, it must match your actual directory structure. So if you have a class com.trilogycomms.Test, then your jar file has a directory 'com', therein is a directory 'trilogycomms', and therein is your Test.class file - exactly the same structure as is built by the compiler in your classes directory.

So in your case where you can't move your source files into a subdirectory, you can add a package prefix to your source directory. By compiling your sources the correct package tree is built by the compiler in your classes directory. And in the end you package the whole directory structure in your classes directory into your jar file.

Great information thanks. So am i right to say that with using a package prefix "com.trilogycomms" I do not need to include a package name in my code? I can then access my classes with say, com.trilogycomms.test, etc.

I generated a jar with the above prefix and included the jar in the module libraries of a new project but when I do an import com.trilogycomms; it can't resolve trilogycomms.

Am I getting warm? :)

Almost: you must add "package com.trilogycomms;" to your classes. The package prefix only prevents you from having to create the same directory structure in the source directory.

Great. I was trying that at the top of my Japplet extension file. But when I do that it does not recognize my other class names which are referenced in my JApplet extension definition. Say, test is the first class ref is does not recognize, I tried to do a
import com.trilogycomms.test; - but it can't resolve this....

You have to set the package for all files in this directory.

Ah!! Ta. Not very change friendly, but I guess that's why they invented grep tools :)

I did that and all compiled. Where my class files were generated in I now get them in ]]>. But no jar file is being generated now...or if it is I can't find it.

Well, most probably you have to tell your jar generator that it shouldn't package class test etc. but directory com.

Thanks. I had my ant build (expermienting with that) working but had to change the jar file path in the build jars dialog.

All's built now. In my 2nd project I include the .jar in the module libraries but when I try and do an import import com.trilogycomms.... it can't resolve trilogycomms......

And you have the whole directory tree in the jar, not only the class files?

Thanks for getting back with me. I rebuilt the 2nd project and initially it seemed to be working fine...but now it can't resolve trilogycomms again :(

the whole directory tree in the jar....hmmmm...i know you say above "And in the end you package the whole directory structure in your classes directory into your jar file"....trying to figure out how to do that....

for ant I have :

]]>
<jar jarfile="$/MyApplet.jar" basedir="$"/>
</target>

is that ok?

IMHO the first line should be ]]>

Perfect! Thanks so much for all.