Can I run from command line program created by Eclipse?

59,183

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
59,183
Roman
Author by

Roman

Updated on July 27, 2022

Comments

  • Roman
    Roman almost 2 years

    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.