Question

First off. I am a newbie and green with respect to Java programming.

I created a Java Eclipse "SWT" "Application Window" on a Eclipse Kepler 4.3.1 (32-bit) running WindowBuilder and SWT (swt.jar). The design view is a composite.

The java application runs fine from Eclipse. I simply press the play button and presto.

Problem: Issuing

java -cp . PkgNetAccelerator32.netAcceleratorApp

yields the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Composite
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Composite
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 6 more

I was doing some research and several posts on other issues suggested that the cure is to create a manifest. I saw an article that said something like:

  • Select the src folder, right click and select export, then Java, then JAR file, specify Manifest.jar and a path on the folder, make sure the "Generate the manifest file" is selected, and press finish.

I created the manifest file and tried launching it with the following command line:

java -jar ../Manifest.jar -cp . PkgNetAccelerator32.netAcceleratorApp

That yielded the same error message above.

I saw another article that said that one should add in the swt.jar file, but that is taken care of by the manifest. No? Specifying "swt.jar" instead of the manifest that I created resulted in an error message:

no main manifest attribute, in ../swt.jar

That error message makes sense.

I am at a loss. How do I run the java application?

I am testing on my Windows 7 Professional 64-bit desktop, but I need to run on CENTOS too. That should be no problem, as I assume whatever the fix on Windows is, will work on CENTOS.

UPDATE: I placed the two lib files in a /lib folder to match the answer given. I then issued the following command line and got the following error message.

C:\SoftDev\Projects\NetAccelerator\JavaWorkspace\NetAccelerator32>java -cp "bin/*;lib/*" PkgNetAccelerator32.netAcceleratorApp
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT     libraries on 64-bit JVM
        at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
        at org.eclipse.swt.internal.Library.loadLibrary(Unknown Source)
        at org.eclipse.swt.internal.C.<clinit>(Unknown Source)
        at org.eclipse.swt.widgets.Display.<clinit>(Unknown Source)
        at PkgNetAccelerator32.netAcceleratorApp.open(netAcceleratorApp.java:43)
        at PkgNetAccelerator32.netAcceleratorApp.main(netAcceleratorApp.java:33)

I found the following article, which produces a command line that works, just that is not reasonable for sending. I much rather use the answer.

Was it helpful?

Solution

Lets assume you have a eclipse project structure similar to this:

Workspace
  -> Project
      -> src (source code directory)
      -> bin (compiled byte code directory as JAR)
      -> lib (jar directory)
      -> .classpath (eclipse file)
      -> .project (eclipse file)

I am assuming that you are exporting the JAR as an executable JAR.

So in order to run a class in that exported JAR with another JAR from the lib directory (swt.jar) while your working directory is the Project directory you will need to execute:

java -cp "bin/*:lib/*" PkgNetAccelerator32.netAcceleratorApp

Using wildcards requires Java 6 or later

Please note: CENTOS requires : as a path separator. Windows uses a semi-colon.

This will add your classes and the swt classes onto the classpath and run your main class PkgNetAccelerator32.netAcceleratorApp

SWT 32bit vs 64bit

You will either need to use a 32bit JVM locally or continue to use a 64bit JVM and use the 64bit version of swt; see swt on windows 64 bit. I would suggest you match your version of your local JVM (32bit or 64bit) with the version that you will be running on the CENTOS machine. You will however need a different swt jar for CENTOS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top