Reputation: 1218
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
Upvotes: 2
Views: 247
Reputation: 36250
Your jar is named Helloworld.jar:
jar cfe HelloWorld.jar HelloWorld HelloWorld.class
But you call it with the package name as prefix to the jar:
java -jar za/ac/uj/csc2a/HelloWorld.jar
java -jar HelloWorld.jar
should be right.
Normally, you put your classes and sources in different directories, if you use packages. A typical tree looks like
proj
|-- src
| |-- za
| |-- ac
| `-- HelloWorld.java
|-- bin
|-- za
|-- ac
`-- HelloWorld.class
You can move to the src-directory, and compile with:
javac -d ../bin za/ac/HelloWorld.java
(reduced the package/path length for convenience) This will produce the directory structure in bin
if needed (or often classes
)
Then, to jar the whole thing with the correct directory structure, you change to proj
, and issue:
jar cfe HelloWorld.jar za.ac.HelloWorld -C bin .
The dot stands for the whole directory.
jar -help
will tell you more options. To create a runnable jar you need a manifest too. Else you can start your app:
java -cp HelloWorld.jar za.ac.HelloWorld
(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 add
Main-Class: za.ac.HelloWorld
with a following blank line. Again: the whole name with package is needed. Main-Class
can'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.
Upvotes: 1
Reputation: 42617
You are creating a jar called HelloWorld.jar:
jar cfe HelloWorld.jar HelloWorld HelloWorld.class
Then trying to invoke a different jar name (za.ac.uj.csc2a.HelloWorld.jar):
java -jar za.ac.uj.csc2a.HelloWorld.jar
They need to be consistent
Upvotes: 0
Reputation: 421110
The -jar
option accepts a filename, not a class.
You should just do
java -jar HelloWorld.jar
But keep in mind that you need to specify a main-class in the manifest file you you want it to execute using the -jar
option.
The manifest file should look like
Main-Class: za.ac.uj.csc2a.HelloWorld
and you include it with the m
option on the jar
command.
Also, it seems that you're adding the .class-file in the root of the jar file. It should be in the za.ac.uj.csc2a
directory
If you don't want to go with a manifest file, you could also launch it like this (perhaps that's what you're trying):
java -cp HelloWorld.jar za.ac.uj.csc2a.HelloWorld
Upvotes: 2
Reputation: 206916
place all the files in the right directories
Are you sure about that? Because if your code is in a package za.ac.uj.csc2a
then your source file must be in a directory za\ac\uj\csc2a
, you should compile it with a command like this:
javac za\ac\uj\csc2a\HelloWorld.java
and also inside the JAR file it should be inside a directory with that name, so you should package the JAR with a command like this:
jar cf HelloWorld.jar za\ac\uj\csc2a\HelloWorld.class
You will only be able to execute it with java -jar
if it is an executable jar, in other words, if you add a manifest file to the JAR with a correct Main-Class
attribute. If you didn't do this, you should be able to run it from the JAR with:
java -cp HelloWorld.jar za.ac.uj.csc2a.HelloWorld
See Packaging Programs in JAR Files for details on how to package your app in an executable JAR file so that you can execute it with
java -jar HelloWorld.jar
Upvotes: 0
Reputation: 951
You should actually call java -jar HelloWorld.jar instead of java -jar za.ac.uj.csc2a.HelloWorld.jar
Upvotes: 0
Reputation: 692003
Your jar file is named HelloWorld.jar, not za.ac.uj.csc2a.HelloWorld.jar. So obviously, java doesn't find it.
The jar file contents must respect the package hierarchy, so inside the jar file, the HelloWorld.class
must be in the folder za/ac/uj/csc2a
.
Upvotes: 0