Results 1 to 13 of 13
Thread: Executable jar file
- 08-25-2010, 04:12 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
Executable jar file
Can the main class be included in a package? And can this package be created into a jar executable?
I was able to create a package with no compile error. I was also able to create a jar file from the *.class files in the package. The jar file is in the CLASSPATH environment variable. But got the following errors when trying to execute the jar file:
java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: PackageTest
Caused by: java.lang.ClassNotFoundException: PackageTest
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: PackageTest. Program will exit.
Thx
- 08-25-2010, 04:17 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 08-25-2010, 09:50 PM #3
Please post the contents of the jar's manifest file.
Also show the package name and class name for the class containing the main() method.
- 08-26-2010, 12:22 AM #4
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
'Executable jar file'
Hi JosAH,
Fellow are the files and comamnds.
Also the test.jar file is in the CLASSPATH environment variable
Thx
PackageTest.java:
package com.bcs.util; // com.bcs.util package
public class PackageTest
{
public static void main(String[] args)
{
while (Console.askYorN("Keep going?") ) {
System.out.println("D 'oh!");
}
}
}
Console.java
package com.bcs.util;
import java.util.Scanner;
public class Console
{
static Scanner sc = new Scanner(System.in);
public static boolean askYorN(String prompt)
{
while (true) {
String answer;
System.out.print("\n" + prompt + " (Y or N)" );
answer = sc.next();
if(answer.equalsIgnoreCase("Y"))
return true;
else if (answer.equalsIgnoreCase("N"))
return false;
}
}
}
The above *.java files are in ~/javaclasses/src.com.bcs.util
The package, test.jar and test.mf are in the ~/javaclasses/com.bcs/util
test.mf:
Main-Class: PackageTest
simon@ohare:~/javaclasses/com/bcs/util> jar tf test.jar
META-INF/
META-INF/MANIFEST.MF
PackageTest.class
Console.class
The jar command is successful:
simon@ohare:~/javaclasses/com/bcs/util> jar cfm test.jar test.mf *.class
simon@ohare:~/javaclasses/com/bcs/util>
simon@ohare:~/javaclasses/com/bcs/util> jar cfm test.jar test.mf *.class
simon@ohare:~/javaclasses/com/bcs/util> java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: PackageTest (wrong name: com/bcs/util/PackageTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader. java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java :616)
at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader .java:283)
at java.net.URLClassLoader.access$000(URLClassLoader. java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: PackageTest. Program will exit.
simon@ohare:~/javaclasses/com/bcs/util>
- 08-26-2010, 01:51 AM #5
The JVM was looking for the short name and found the longer name with a package path.NoClassDefFoundError: PackageTest (wrong name: com/bcs/util/PackageTest)
Your class is in a package. You need to include the package path in the Main-Class field:
Main-Class: com.bcs.util.PackageTest
- 08-26-2010, 03:35 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
'Executable jar file'
Hi Norm,
I make the change to the test.mf file, but I still have the same problem. I remember also doing just that before posting this problem
Thx
simon@ohare:~/javaclasses/com/bcs/util> jar cmf test.jar test.mf *.class
simon@ohare:~/javaclasses/com/bcs/util> java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: PackageTest (wrong name: com/bcs/util/PackageTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader. java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java :616)
at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader .java:283)
at java.net.URLClassLoader.access$000(URLClassLoader. java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: PackageTest. Program will exit.
- 08-26-2010, 03:48 AM #7
Please show the contents of the manifest file that is in the jar file.
Also you need to position the current directory when the jar command is issued to include the package path to the class file so that it is in the jar file. You should be in the folder containing the com folder. The reference to the class files should include the path to them. Here's some lines from a batch file I use to create a jar file:
HereSET JarName=%DEV_HOME%\JavaDevelopment\HTTPServer\Serv lets.jar
%DEV_DRIVE%
cd %DEV_HOME%\JavaDevelopment\HTTPServer\
jar -cfm %JarName% servlets\Servlets.mnf servlets\*.class servlets\cart\*.class servlets\database\*.class servlets\htmlchat
cd %DEV_HOME%\JavaDevelopment\HTTPServer\servlets\
jar -uf %JarName% rmibook\*.class rmibook\chat\*.class rmibook\chat\Client\*.class rmibook\chat\Server\*.class rmibook\database\*.class rmibook\database\DBServer\*.class rmibook\timeserver\*.class rmibook\util\*.class
- 08-26-2010, 04:46 AM #8
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
Hi Norm,
Here is the content of the manifest file and a listing of the current directory
where the jar executable is made. But I still get the same problem. The test.jar file is also in the CLASSPATH.
Thx
simon@ohare:~/javaclasses/com/bcs/util> jar tf test.jar
META-INF/
META-INF/MANIFEST.MF
Console.class
PackageTest.class
simon@ohare:~/javaclasses/com/bcs/util> ls -lt
total 16
-rw-r--r-- 1 simon users 2659 2010-08-25 22:18 test.mf
-rw-r--r-- 1 simon users 563 2010-08-25 22:15 PackageTest.class
-rw-r--r-- 1 simon users 982 2010-08-25 22:15 Console.class
-rw-r--r-- 1 simon users 1448 2010-08-25 19:57 test.jar
simon@ohare:~/javaclasses/com/bcs/util>
simon@ohare:~/javaclasses/com/bcs/util> jar -cfm test.jar test.mf /home/simon/javaclasses/com/bcs/util/*.class
simon@ohare:~/javaclasses/com/bcs/util> java -jar test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/bcs/util/PackageTest
Caused by: java.lang.ClassNotFoundException: com.bcs.util.PackageTest
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: com.bcs.util.PackageTest. Program will exit.
simon@ohare:~/javaclasses/com/bcs/util>
- 08-26-2010, 07:36 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 08-26-2010, 09:38 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
simon@ohare:~/javaclasses/com/bcs/util> jar tf test.jar
You are jarring the class files, but ignoring the package structure.
You need to do as Norm said and jar from the package (in your case from the javaclasses directory).
- 08-26-2010, 12:46 PM #11
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
'Executable jar file'
Thx To Norm and Jos
The below works. The package directory is the directory that contains the package. It can be very confuse since a package has the form as a directory.
Thx again
simon@ohare:~/javaclasses> jar cfm test.jar com/bcs/util/test.mf com/bcs/util/*.class
simon@ohare:~/javaclasses> jar tf test.jar
META-INF/
META-INF/MANIFEST.MF
com/bcs/util/Console.class
com/bcs/util/PackageTest.class
simon@ohare:~/javaclasses> java -jar test.jar
- 08-26-2010, 12:57 PM #12
Yes it is that.package has the form as a directory.
packages and classpath are one of the problems students have. Hopefully you've got it now and can move on. Write a script to create the jar file and you'll have something to refer back to if you forget how later.
- 08-26-2010, 01:03 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Yup, that's what we were trying to tell you all the time ;-) The system class loadee uses directory structures (optionally in a .jar) as the only means of determining the package structure. If you had used your own classloader you could've used your own structure. The class loader loads a class a.b.yourClass by looking for an entry yourClass.class in directories a/b/ pointed to by the classpath value(s). Simple and consistent ...
kind regards,
Jos
Similar Threads
-
How to create java executable file through CMD
By Riaz Ali in forum New To JavaReplies: 4Last Post: 08-03-2010, 08:20 AM -
Executable File.
By BeeGee in forum Advanced JavaReplies: 13Last Post: 05-25-2010, 12:35 PM -
Exporting to a executable JAR file
By Drun in forum EclipseReplies: 4Last Post: 03-27-2010, 03:16 PM -
Executable Jar File Creation
By Doctor Cactus in forum New To JavaReplies: 7Last Post: 11-13-2008, 03:31 AM -
Generate an executable file
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks