• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to deploy and run a java executable with package?

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
qionghua yang
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the class directory in your classpath???
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic