29

Using Eclipse I have created a SWT Hello World program. I was able to run this program from Eclipse and it worked fine.

In the "/home/myname/workspace/HelloWorldSWT" I found two files: HelloWorldSWT.java and HelloWorldSWT.class. I wanted to execute the corresponding program from the command line. First I tried to type "java HelloWorld" and I got the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
   at gnu.java.lang.MainThread.run(libgcj.so.90)
Caused by: java.lang.ClassNotFoundException: HelloWorld not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
   at java.net.URLClassLoader.findClass(libgcj.so.90)
   at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at gnu.java.lang.MainThread.run(libgcj.so.90)

I also tried this "java -cp /home/roman/workspace/ HelloWorld.HelloWorld". As the result I got the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError: loaded class HelloWorld.HelloWorld was in fact named HelloWorld
   at java.lang.VMClassLoader.defineClass(libgcj.so.90)
   at java.lang.ClassLoader.defineClass(libgcj.so.90)
   at java.security.SecureClassLoader.defineClass(libgcj.so.90)
   at java.net.URLClassLoader.findClass(libgcj.so.90)
   at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at java.lang.ClassLoader.loadClass(libgcj.so.90)
   at gnu.java.lang.MainThread.run(libgcj.so.90)

Does anybody know what I am doing wrong? Thank you in advance for any help.

Roman
  • 124,451
  • 167
  • 349
  • 456

9 Answers9

69

Go to the Debug perspective, and select the program you just ran (where it says Terminated, exit value... in the Debug tab) Right click, and choose Properties, there you can see the whole command line command that was launched by eclipse.

You can run this same command in the same directory eclipse did (see in Run Configurations, Arguments, Working directory) and it will work.

Community
  • 1
  • 1
Persimmonium
  • 15,593
  • 11
  • 47
  • 78
  • 1
    I cannot find the "Debug perspective". Where it is? – Roman Feb 16 '10 at 21:09
  • Choose to add a perspective and you can choose the Debug one among others – Persimmonium Feb 16 '10 at 21:38
  • 1
    +1. Simplest way to find complete command to run. I had to change javaw.exe to java.exe, removed some unnecessary switches and it worked like a charm. – understack Jan 04 '11 at 07:00
  • 1
    "" is in the "Debug" tab, there you need to right click. took me quite a while... – mb21 Oct 04 '12 at 14:52
  • What a great tip thank you. I too had to execute java instead of javaw but the classpath etc. that was in that command line made it work. Saved me so much time!!! – Bitwyse1 Apr 25 '16 at 18:43
  • @Persimmonium Can you please give any further info as to "how" this command line is executed by Eclipse (simulating the typing of "java" command by hand in command line and pressing Enter)? What is the exact portion/feature of Java and/or Windows OS that is used by Eclipse to get the same result ? – user104309 Aug 25 '17 at 22:41
7

You must add the directory where eclipse is storing the HelloWorldSWT.class file in the classpath. It is defined when the project is created, like "bin", "build" or "classes". Check the project properties or search the HelloWorldSWT.class file.

Assuming it is the build directory inside the HelloWorldSWT workspace and that your class is in no package (default package), the command should be:

java -cp /home/roman/workspace/HelloWorldSWT/build HelloWorldSWT

or just change to that directory and use:

java -cp . HelloWorldSWT

EDIT:
The -cp . means that only the actual directory is used for finding the class files. You must also add all libraries (JARs or other directories) used by your program (databse, SWT, ...).
For more details you can have a look at the documentation: How Classes are Found, Setting the Classpath and java comand

user85421
  • 28,957
  • 10
  • 64
  • 87
  • I found the HelloWorldSWT.class. It is located in "/home/myname/workspace/HelloWorldSWT/". I go to this directory and type "java -cp . HelloWorldSWT". As the result I get an error message: Exception in thread "main" java.lang.NoClassDefFoundError: org.eclipse.swt.widgets.Display – Roman Feb 17 '10 at 08:56
  • @Roman: that is the next problem: you must add all needed libraries to the `CLASSPATH` environment variable or to the `-cp` argument; example: `java -cp .:lib/somelib.jar:... HelloWorldSWT`. I edited my answer adding a link to the documentation (I'm sure the CLASSPATH question is answered somewhere else on stackoverflow.com) – user85421 Feb 17 '10 at 13:24
4

I do some workaround so as to take full advantage of the Eclipse convenience. Below is what I found, and it worked well for me. Hope it will help: enter image description here

foxwendy
  • 2,819
  • 2
  • 28
  • 50
3

Seems to me you don't have a class named HelloWorldSWT``, but rather a class named HelloWorldSWT in a package named HelloWorldSWT (you can confirm this by going at the first line of HellowWorldSWT.java, where you will find package HelloWorldSWT;

If so, go in parent directory and type

java HelloWorldSWT.HelloWorldSWT This would work.

Riduidel
  • 22,052
  • 14
  • 85
  • 185
2

It is possible that you are not loading the SWT library correctly, and as a result your class fails to load.

The SWT library is part of jars that are already loaded when you run Eclipse but are not loaded in a command line parameter. Did you modify your class path accordingly?

Here is an old article about how to do this sort of stuff in older versions of Eclipse http://www.ibm.com/developerworks/opensource/library/os-ecgui1/ You will need different jars today with latest version fo Eclipse. It might even come down to a single jar.

Check out the SWT FAQ; at least for Mac Carbon, you can use a single jar, I would bet you can do that for other platforms.

Also, I'm not 100% sure that you can run Eclipse under openJDK, which seems to be the case on your platform.

Uri
  • 88,451
  • 51
  • 221
  • 321
  • Thank you for the answer. I am a newbie in Java and I do not understand the essence. I mean, it looks strange to me that we need to use some tricky ways to run SWG applications without Eclipse. What is the use of the program written in Eclipse if it can be easily executed only within the Eclipse? – Roman Feb 16 '10 at 21:00
  • If you want some easy running GUI application in Java, don't use SWT, but Swing. If you use SWT, you have to manage its dependencies. When creating an RCP application, the platform manages these dependencies for you, but in case of standard programs you have to depend on the SWT jars using the classpaths. – Zoltán Ujhelyi Feb 17 '10 at 11:00
1

If you want to run the program with command line arguments from within Eclipse you can go to Run->Run Configurations, which will bring up a window with the program you're running as well as a box where you can enter arguments.

Ian
  • 599
  • 2
  • 5
  • 13
1

I found another quick and dirty solution if you just want to see some output on the command line. But this is not a good practice for the long term!

  1. Remove the package declaration in your code (Eclipse will complain and give you a red cross, ignore that first), e.g. package hello;

  2. Save.

  3. Open your command line and navigate until you are in the src folder (use cd).

  4. Compile the java file, e.g. javac HelloWorld.java

  5. Execute the java class file, e.g. java HelloWorld

It should work, as long as you are in the correct directory without any package declarations in the code! The package declarations do cause issues..and Eclipse is set up to make sure everything works so that's why it's fine to have the package declarations there. An alternative workaround is to really go and set the classpath on your own (which always confuses me every time I have to do it, which I rarely do).

Kevin Lee
  • 2,307
  • 26
  • 31
1

This is the problem which is caused because the JVM is not able to find the HelloWorld class as you have the class name as HelloWorldSWT with main method in it. try with

java HelloWorldSWT

GuruKulki
  • 25,776
  • 50
  • 140
  • 201
  • I tried "java -cp /home/roman/workspace/ HelloWorldSWT.HelloWorldSWT" and "java HelloWorldSWT". Both did not work. – Roman Feb 16 '10 at 21:06
1

You have not set classpath correctly then.

I think it is usually more convenient to have the IDE build a jar-file with all non-jre libs included and execute that from the command-line.

java -jar myprogram.jar

What is the use of the program written in Eclipse if it can be easily executed only within the Eclipse?

That's really a good question, and deployment of java applications is really an art in itself. Both compilation and execution is - if not complicated - at least cumbersome when performed by hand. But Eclipse and other tools like Netbeans can help you perform these tasks in an easy way, and even package up your program for you so that others that do not use these tools also can execute the programs easily.

deleted
  • 195
  • 2