• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] compile/run with packages

This topic is 6219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m compiling, jarring and running from the command line. If I don''t have my classes in a package, then everything works fine, i.e. I use javac,jar and java to compile, make a jar file, and then run a class from that jar. But if I put my classes in a package, they won''t run. It says something like "Exception in main: Editor (package/Editor has wrong name)." So it looks like the class files have the package name in them correctly. When I open up the jar file with winzip, I notice that they are not in the path /package, which they should be. So my question is how do I jar these up into the correct format? thanks

Share this post


Link to post
Share on other sites
Advertisement
If your package was called ''myClasses'' then your classes would have to be in a directory ''myClasses/''
and I would then run with ''java myClasses.className''.
Yes the class files must live in a directory with the same name as their package, and this directory must be included in your class_path...

Share this post


Link to post
Share on other sites
Thats the problem... the classes are not in the correct path within the jar file. So my question is how do I get them to have the correct path within the jar file?

Share this post


Link to post
Share on other sites
You need to create the jar file from the unnamed package level...

If you had packageA and packageB, and wanted them both in one jar file, you''d do something like this, from the directory which has the packageA and packageB directories as immediate subdirectories:

jar cvf packages.jar packageA/*.class packageB/*.class

Or, *.* if you want other files, etc...

Share this post


Link to post
Share on other sites

  • Advertisement