File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to deploy and run a java executable with package? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to deploy and run a java executable with package?" Watch "How to deploy and run a java executable with package?" New topic
Author

How to deploy and run a java executable with package?

qionghua yang
Ranch Hand

Joined: Oct 31, 2000
Posts: 68
Hi,
I have several java class files under one package. I would like to bundle this package and deploy an executable jar file to run. The class with the main program is Controller_MainProgram. What is the manifest file? What is the command of jaring the files? And how to run the jar file?
when I run the jar file, I have NoMainException error which I think it is because of the package.
Thanks!
qionghua
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The manifest file is the place where you tell the JVM which class to look for the main in (many of the classes in the jar may have mains, but only the one listed in the manifest will be used to kick off the application).

Everything that you ever wanted to know about jar files: http://developer.java.sun.com/developer/Books/JAR/index.html


"JavaRanch, where the deer and the Certified play" - David O'Meara
qionghua yang
Ranch Hand

Joined: Oct 31, 2000
Posts: 68
Thanks for your advise, cindy.
My problem is:
For example, I have two class files named: One.class and Two.class. Two.class has the main method. They belong to a package named: package01.
So the source code for One.jar is like:
package package01;
public class One {
...........
}
And the source code for Two.jar is like:
package package01;
public class Two {
...........
public static void main(String args[]) {
}
}
I would like to deploy this application. So I follow these steps:
1) Make a manifest file named Manifest.mf
Main-Class: package01.Two.class
2) Make a jar file named: application.jar
jar.exe cvfm application.jar Manifest.mf One.class Two.class
3) Run the jar file:
java.exe -jar application.jar
It gave me the following error: "Exception in thread "main" java.lang.NoClassFoundError: prototype02/Two ".
My file structure is:
D:\project\src\package01\One.java
\Two.java
\class\package01\One.class
\Two.class
\Manifest.mf
\application.jar
I don't know exactly where went wrong. Please help! Thanks a lot!
qionghua


qionghua yang
Ranch Hand

Joined: Oct 31, 2000
Posts: 68
OOps, I made some typing mistakes of last one. Forget about last one.
***************************************************************
Thanks for your advise, cindy.
My problem is:
For example, I have two class files named: One.class and Two.class. Two.class has the main method. They belong to a package named: package01.
So the source code for One.java is like:
package package01;
public class One {
...........
}
And the source code for Two.java is like:
package package01;
public class Two {
...........
public static void main(String args[]) {
}
}
I would like to deploy this application. So I follow these steps:
1) Make a manifest file named Manifest.mf
Main-Class: package01.Two
2) Make a jar file named: application.jar
jar.exe cvfm application.jar Manifest.mf One.class Two.class
3) Run the jar file:
java.exe -jar application.jar
It gave me the following error: "Exception in thread "main" java.lang.NoClassFoundError: prototype02/Two ".
My file structure is:
D:\project\src\package01\One.java
D:\project\src\package01\Two.java
D:\project\class\package01\One.class
D:\project\class\package01\Two.class
D:\project\class\package01\Manifest.mf
D:\project\class\package01\application.jar
I don't know exactly where went wrong. Please help! Thanks a lot!
qionghua

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You were sitting in your package01 directory when you ran the jar command. It bundled everything up and put the jar file in the package01 directory. Now you run from that directory. The first thing that the JVM does it look for the subdirectory of the CURRENT directory that matches the package name. It is looking for the package01 subdirectory of package01.
Try moving your jar file up to class directory and run from there. In theory you should be sitting in the class directory when you create the jar file and just use a wildcard for the file names. It would pull in the classes in the class directory and the package01 subdirectory with all of it's packages.
It would probably be better to name the class directory "application" instead. I have an application mpl.jar that sits in the ...\mpl directory that has lots of packages in it in assorted subdirectories of \mpl.
qionghua yang
Ranch Hand

Joined: Oct 31, 2000
Posts: 68
Thanks cindy. I have tried it but it seemed I didn't get any luck on that. This is what I did.
1) I created the Manifest.mf on D:\project\class\;
2) In D:\project\class, Jar all the class files under package01 using the following command:
jar.exe cvfm application.jar Manifest.mf -C package01/ .
This will place the application.jar file under class directory.
3) Run jar file under D:\project\class:
java.exe -jar application.jar
Thanks!
qionghua
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Is the class directory in your classpath???
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Just out of curiosity, did you put your manifest in any other subdirectory than the one it needs to be in when you jar your stuff up?
It should be in:
\project\class\META-INF\MANIFEST.MF
qionghua yang
Ranch Hand

Joined: Oct 31, 2000
Posts: 68
Thanks Cindy.
What do you mean classpath? Do I need to specify the classpath when I run the jar file? How to?
And in fact, I don't have another subfolder to hold the manifest file only. I put the manifest in the same directory as the class files. What is the advantage of using a separate subdirectory for the manifest file?
Thanks!
qionghua
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
In your autoexec.bat (at least on windows) there is a path parameter to tell the JVM where to find the executables like java.exe etc., and you need to put in a classpath to tell the JVM where to look for class and jar files.
Go to DOS and type SET at the prompt and look for the 2 parameters. Your classpath should be set to
D:\project\class\
If you still have problems you can try explicitely adding your jar to the classpath
D:\project\class\;D:\project\class\application.jar
In the article that I posted above they talk about where the system puts the manifest file. You need to leave it there.
qionghua yang
Ranch Hand

Joined: Oct 31, 2000
Posts: 68
Hi Cindy, thanks a lot for your help. We made a batch file to show the manifest file, make the jar file and run the jar file. Notice that I have made some changes in my file structure. I have adopted your suggestion in my file struture. So I have all the class files in D:\Javadevel\hipPocket\application\package01 directory. I create the Meta-Inf subfolder for manifest file.
And here is the result of the batch file. It looke like I am still missing something very simple here.
***************************************************************
> cd D:\JavaDevel\hipPocket\application
> BatchMain6.bat

D:\JavaDevel\hipPocket\application>BatchMain6.bat
D:\JavaDevel\hipPocket\application>rem this file is: d:\JavaDevel\hipPocket\application\BatchMain.bat
D:\JavaDevel\hipPocket\application>rem ----- ----- current working directory is ...application
D:\JavaDevel\hipPocket\application>rem ----- ----- locate the jar file in .
D:\JavaDevel\hipPocket\application>rem ----- ----- use: .\meta-inf\package01.startTwo.mf
D:\JavaDevel\hipPocket\application>rem -----show that the program works using the class files
D:\JavaDevel\hipPocket\application>C:\JBuilder4\jdk1.3\bin\java.exe -cp .\package01 Two
*******************WORKS Springfield
D:\JavaDevel\hipPocket\application>rem -----show the manifest file
D:\JavaDevel\hipPocket\application>type .\meta-inf\package01.startTwo.mf
Main-Class: package01.Two
D:\JavaDevel\hipPocket\application>rem -----build the archive file (jar) using .\meta-inf\package01.startTwo.mf
D:\JavaDevel\hipPocket\application>C:\JBuilder4\jdk1.3\bin\jar.exe cvfm .\Package01.jar .\meta-inf\package01.startTwo.mf package01\*.class
added manifest
adding: package01/One.class(in = 1437) (out= 760)(deflated 47%)
adding: package01/Two.class(in = 862) (out= 493)(deflated 42%)
D:\JavaDevel\hipPocket\application>rem -----show the contents of the jar file
D:\JavaDevel\hipPocket\application>C:\JBuilder4\jdk1.3\bin\jar.exe tf .\Package01.jar
META-INF/
META-INF/MANIFEST.MF
package01/One.class
package01/Two.class
D:\JavaDevel\hipPocket\application>rem -----two tries: run the main program using the jar file
D:\JavaDevel\hipPocket\application>C:\JBuilder4\jdk1.3\bin\java.exe -jar -cp . .\Package01.jar
java.lang.NoClassDefFoundError: package01/Two (wrong name: Two)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:111)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
Exception in thread "main"
D:\JavaDevel\hipPocket\application>C:\JBuilder4\jdk1.3\bin\java.exe -jar -cp .\package01 .\Package01.jar
java.lang.NoClassDefFoundError: package01/Two (wrong name: Two)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:111)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
Exception in thread "main"
Thanks for your help. I really appreciate it.
qionghua

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
OK so now I a REALLY confused.
First I didn't realize that you were using JBuilder and I do not know what complications that adds.
Next, I thought that you were creating a "application.jar" file to sit in the .\application subdirectory. But now I see that you are instead using Package01.jar That is a directory lower that we WERE talking about.
Also why do you have two META-INF/ and
META-INF/MANIFEST.MF directories??
Then in
D:\JavaDevel\hipPocket\application>C:\JBuilder4\jdk1.3\bin\jar.exe cvfm .\Package01.jar .\meta-inf\package01.startTwo.mf package01\*.class
should be
D:\JavaDevel\hipPocket\application>C:\JBuilder4\jdk1.3\bin\jar.exe cvfm .\Package01.jar .\meta-inf\package01.startTwo.mf *
which should pick up all the classes and subdirectories below, including the Package01 subdirectory.
Last, I did not know that using package01.startTwo.mf would work. "Seems" to have though.
qionghua yang
Ranch Hand

Joined: Oct 31, 2000
Posts: 68
Hi Cindy,

Wow, finally I got it. It works perfect now. Thanks a lot cindy. You have been such a great help to me. I really appreciate it. I know it is very easy to get confused. Even myself gets confused. But now I try your strategy and suddenly everything is so clear to me.

Thank you so much.
qionghua
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to deploy and run a java executable with package?