1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

JARing a java project with main class in bin

Discussion in 'Software Development' started by gdi1942, Jul 15, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. gdi1942

    gdi1942 Thread Starter

    Joined:
    Nov 1, 2006
    Messages:
    215
    I'm attempting to JAR my project and have it be executable. Theres a problem with the way it needs to do it. Ecclipse automatically make s abin file to hold your class files in. After compiliation it throws them in there, fine. I never put "package bin;" at the top of any of my classes and it runs without a flaw in ecclipse. However to run the main class in command line you need to be in the bin folder to launch. And to jar the entire project I figured you had to be in the root of the project in command line and specifing this command

    >jar cmf Manifest.txt MyProject.jar *

    with Manifest in the folder and contains
    Main-Class: bin.AppDriver <Carrage Return>

    but then i go to execute the jar using this command: java -jar MyProject.jar

    I get the following error.
    Exception in thread "main" java.lang.NoClassDefFoundError: bin/AppDriver (wrong
    name: AppDriver)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    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:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Hi gdi1942,

    I don't use Eclipse, so I can't tell about the way it builds JAR files.
    In the Manifest file, I would remove the reference to a bin package from the Main-Class parameter as it seems that Eclipse executes your application without any package reference in your coding.
     
  3. gdi1942

    gdi1942 Thread Starter

    Joined:
    Nov 1, 2006
    Messages:
    215
    the manifest now reads
    Main-Class: AppDriver <Carrage Return>

    Java spits out.
    Exception in thread "main" java.lang.NoClassDefFoundError: AppDriver

    which would make sense as the AppDriver is in the bin folder inside the jar
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Assuming your application contains many classes, you'll need to make a package statement at the top of each of the sources.
    Then you compile all the classes of your application and you store all the resulting .class files in the same directory which name is the name of the package.
    Adapt the Main-Class parameter of the Manifest file and jar your application with the C option to point the directory where the classes are stored.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/596029

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice