Problem: I wrote a hello world java program, compiled and created a jar file of it – PERFECT, no problems there.
I now added the following to the top of the source code
package za.ac.uj.csc2a;
saved the code, place all the files in the right directories and every time i compile with this batch file
del *.class
del *.jar
del *.txt
echo compiling HelloWorld.java.....
javac HelloWorld.java
javap -c HelloWorld.class > ByteCode.txt
jar cfe HelloWorld.jar HelloWorld HelloWorld.class
java -jar za.ac.uj.csc2a.HelloWorld.jar
i get,
Error: Unable to acces jarfile za.ac.uj.csc2a.HelloWorld.jar
Any Advice….
Here is the final bat file that works
del *.class
del *.jar
del *.txt
RD /Q /S bin
echo compiling HelloWorld.java.....
MD bin
javac -d bin za/ac/uj/csc2a/HelloWorld.java
javap -c bin/za/ac/uj/csc2a/HelloWorld.class > ByteCode.txt
jar cfe HelloWorld.jar za.ac.uj.csc2a.HelloWorld -C bin .
java -jar HelloWorld.jar
pause
thanks alot
Aiden
Your jar is named Helloworld.jar:
But you call it with the package name as prefix to the jar:
should be right.
Normally, you put your classes and sources in different directories, if you use packages. A typical tree looks like
You can move to the src-directory, and compile with:
(reduced the package/path length for convenience) This will produce the directory structure in
binif needed (or oftenclasses)Then, to jar the whole thing with the correct directory structure, you change to
proj, and issue:The dot stands for the whole directory.
will tell you more options. To create a runnable jar you need a manifest too. Else you can start your app:
(shortened the path a bit again).
Note, that the structure of the directory has to reflect the package declaration. You can’t omit a part (
za), and step into the next directory (ac). The class to run is za.ac.HelloWorld and therefor it is searched for in za/ac/ – no matter whether in the file system or in the jar.If you learn to issue the commands from the right base, it is easy. If not, you’ll get years of trial and error.
For a jar which can be startet with
java -jar, you need a manifest. A template is generated by the jar command. There you addwith a following blank line. Again: the whole name with package is needed.
Main-Classcan’t be written as Mainclass. HelloWorld must not be followed by .class or .java – common mistakes! But as you know, you may use the -e switch to specify the main class, but again: Include the package name.