[java] compile/run with packages

Started by
2 comments, last by gdalston 23 years, 3 months ago
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
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...
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?
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...

This topic is closed to new replies.

Advertisement