Results 1 to 13 of 13
  1. #1
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by franklyn@bellsouth.net View Post
    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
    Where did it get that 'PackageTest' thing from? Have you put that in your manifest file? You are just giving your .jar file so the jvm should read a manifest file and find a main class in there, package name and all.

    kind regards,

    Jos

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,564
    Rep Power
    25

    Default

    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.

  4. #4
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default '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>

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,564
    Rep Power
    25

    Default

    NoClassDefFoundError: PackageTest (wrong name: com/bcs/util/PackageTest)
    The JVM was looking for the short name and found the longer name with a package path.

    Your class is in a package. You need to include the package path in the Main-Class field:
    Main-Class: com.bcs.util.PackageTest

  6. #6
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default '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.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,564
    Rep Power
    25

    Default

    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:
    SET 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
    Here

  8. #8
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default

    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>

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by franklyn@bellsouth.net View Post
    META-INF/
    META-INF/MANIFEST.MF
    Console.class
    PackageTest.class
    I can see an entry 'PackageTest.class' in your .jar but no 'com/bcs/util/PackageTest.class' entry. You have stored a wrong file in your .jar.

    kind regards,

    Jos

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    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).

  11. #11
    Join Date
    Aug 2010
    Posts
    7
    Rep Power
    0

    Default '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

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,564
    Rep Power
    25

    Default

    package has the form as a directory.
    Yes it is that.
    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.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by franklyn@bellsouth.net View Post
    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
    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

  1. How to create java executable file through CMD
    By Riaz Ali in forum New To Java
    Replies: 4
    Last Post: 08-03-2010, 08:20 AM
  2. Executable File.
    By BeeGee in forum Advanced Java
    Replies: 13
    Last Post: 05-25-2010, 12:35 PM
  3. Exporting to a executable JAR file
    By Drun in forum Eclipse
    Replies: 4
    Last Post: 03-27-2010, 03:16 PM
  4. Executable Jar File Creation
    By Doctor Cactus in forum New To Java
    Replies: 7
    Last Post: 11-13-2008, 03:31 AM
  5. Generate an executable file
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •