4

I generated a very simple runnable jar file using Eclipse's "Export-->Java-->Runnable Jar File" function. My HelloWorld class looks like this:

import javax.swing.JFrame;
public class HWorld extends JFrame {
  public static void main(String[] args) {
    new HWorld();
  }
  public HWorld() {
    this.setSize(200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Hello World!");
    this.setVisible(true);
  }
}

Now, after generating the .jar file , it runs fine from the command line using the command "java -jar HWorld.jar"

But, when I try to execute the jar on its own (which supposedly should work) I get the following error and I don't know why:

E:\Eclipse\workspace>HWorld.jar
Exception in thread "main" java.lang.NoClassDefFoundError: E:\Eclipse\workspace\HWorld/jar
Caused by: java.lang.ClassNotFoundException: E:\Eclipse\workspace\HWorld.jar
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: E:\Eclipse\workspace\HWorld.jar.  Program will exit.

My manifest looks like this:

Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Rsrc-Main-Class: HWorld
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

The only thing that looks really fishy to me is this (since a .jar is not a .class):

Could not find the main class: E:\Eclipse\workspace\HWorld.jar

Looking for ideas or thoughts or even an answer! I tried to give as much info as possible in hope of a quality answer. This thread implies that it should work but doesn't answer my question: http://forums.oracle.com/forums/thread.jspa?threadID=2152988 . Can anyone else try it in their Eclipse?

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • Since I originally posted this question I now do this .jar action using Maven instead of trying to do it with Eclipse. – djangofan Jun 06 '14 at 23:04

3 Answers3

9

Jars are never 'executable' in this sense. What this jar is good for is:

java -jar YOURJAR.jar

update the backtrace you supplied is bizarre. It appears that Windows decided to go ahead and launch your jar with some version of Java, but pass it a pathname in the place of a class name. I don't know what the story is with that.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • the jar contains the org.eclipse.jdt.internal.jarinjarloader classes and so IT IS launchable this way, or so it appears that it should be. (read the manifest file entries above) – djangofan Feb 02 '11 at 23:44
  • im not too sure about your answer and at the same time I believe it. now, when I execute the .jar file without using "java" then , how is it that the interpreter seems to be running? it seems to me that it must be working to a certain degree? – djangofan Feb 03 '11 at 00:00
  • yeah, i see what you mean. in the manifest I am certainly using "./" for local directory and it looks like the jarinjarloader is translating it into an absolute path. i might try to find the source code for "jarinjarloader" if thats possible. – djangofan Feb 03 '11 at 05:12
  • since jarinjarloader is something created at IBM, i tried downloading the IBM version of Eclipse but it doesnt contain the Export runnable jar option. – djangofan Feb 03 '11 at 05:47
  • Im starting to believe that your correct but why do people refer to launching a .jar file (as you suggest above) as executable when its really the java.exe interpreter that is the executable part and NOT the .jar ? I feel like we are overlooking something here and that creating a self launching .jar may indeed be possible. – djangofan Feb 03 '11 at 18:23
  • This may be a while but thanks. It was what what I was looking for :) – Lews Therin Nov 14 '11 at 21:47
  • I wrote a script to setup the file association on windows (so that a jar can be double-click-executed) : https://gist.github.com/djangofan/4144970#file-jar_association-bat – djangofan May 23 '13 at 15:46
2

Uninstalling all older java versions on my machine fixed the issue for me.

At my end I was able to run the .jar file with the command line, but not with the default double-click option. Afterwards the latter was working again

0

In my case, by mistake I had not declared the class containing public static void main() as a public class. After declaring the class as public was able to resolve this issue with the next export as runnable jar via eclipse...

Hope this helps...