Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runnable jar file generated by Eclipse wont execute?

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?

like image 223
djangofan Avatar asked Dec 22 '22 18:12

djangofan


2 Answers

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.

like image 120
bmargulies Avatar answered Jan 15 '23 01:01

bmargulies


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

like image 26
Rick Hindriks Avatar answered Jan 15 '23 00:01

Rick Hindriks