1

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.

Community
  • 1
  • 1
Sarah Weinberger
  • 15,041
  • 25
  • 83
  • 130
  • You are specifying the `classpath` (`-cp`) as `.`, do you have the `swt.jar` in the working directory? What files are in the working directory? – Craig Dec 30 '13 at 23:48
  • The current/working directory is the contents of the project's bin folder. I placed the two jar files in the parent folder. Java did not like the jar files in the same folder as the file to run. – Sarah Weinberger Dec 31 '13 at 00:00
  • I presume the solution is a simple one. I am trying to run my first Java Application window from the command line, so this part of the development process is brand new to me. – Sarah Weinberger Dec 31 '13 at 00:01

1 Answers1

1

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.

Community
  • 1
  • 1
Craig
  • 1,390
  • 7
  • 12
  • Craig, that gets me closer, just get the 32-bit on 64-bit, not sure where that comes from as I have both JVMs installed on my machine. See updated text question. – Sarah Weinberger Dec 31 '13 at 00:29
  • 1
    See my update, however you likely have the 64 bit JVM on the `PATH` environment variable which makes the `java` command automatically use the 64 bit JVM. You can either change your `PATH` environment variable to point to the 32 bit JVM or you can just put in the full path to the 32 bit version of the `java` command: eg. `"C:\Program Files (x86)\Java\jdk1.6.0_31\bin\java.exe"` – Craig Dec 31 '13 at 00:43
  • CentOS issue and Windows OK: Java was not in my path, so not sure where Java got pulled from, however using the full path to the 32-bit command line worked on my Windows box like a charm. I still have a problem on my CENTOS box. I copied over the /bin and the lib folder. I executed the same command line (just no full path to java) and received the error "Error: Could not find or load main class PkgNetAccelerator32.netAcceleratorApp". That seems like a missing WindowBuilder thing or something like that. The Linux version of the swt.jar file is in the lib folder. – Sarah Weinberger Dec 31 '13 at 21:33
  • Same problem on CENTOS. I tried adding in the artifacts.jar and the content.jar to the lib folder form the "WB_v1.6.1_UpdateSite_for_Eclipse4.4.zip" file that I downloaded. These two files are the same ones that I am using and references on Windows. The swt.jar, as mentioned, is specific to Linux on the CENTOS box, and the Manifest.jar is what I created. These are all the files now, so not sure what the issue is on CENTOS. – Sarah Weinberger Dec 31 '13 at 21:56
  • Make sure you are issuing the java command from the correct directory (likely the parent directory of `/bin` and `/lib`). If this doesn't work, could you show the exact command you are running? – Craig Jan 01 '14 at 23:00
  • I created a folder called Java on the CENTOS (v6.5, 64-bit) box, where I placed the /bin and /lib folders. This Java folder is located at hard path "/home/jmr/Documents/NetAccelerator/Java". The /lib folder has the Linux version of "swt.jar". The exact command line is: java -cp "bin/*;lib/*" PkgNetAccelerator32.netAcceleratorApp". For whatever it is worth I added artifacts.jar and content.jar to the lib folder. That folder also has Manifest.jar and resty-0.3.2.jar. Thanks for helping out Craig. – Sarah Weinberger Jan 02 '14 at 15:13
  • Okay, I got the thing to work. Yay! I had to download the 64-bit SWT using "More..." on CENTOS. The other problems were caused by a semi-colon and not a colon in the -cp option. The working command line on CENTOS is: "java -cp "bin/*:lib/*" PkgNetAccelerator32.netAcceleratorApp". – Sarah Weinberger Jan 02 '14 at 21:57