7
votes

I've got a simple java program, socket, AWT and jUnit dependencies.

I've built it in eclipse and when I run/debug it from eclipse it works.

When I launch it from the command line I get an error saying

Exception in thread "main" java.lang.NoClassDefFoundError

I'm guessing it's due to not finding one of the required dependencies in the classpath.

Given that launching from eclipse works, Is there a way of determining what command line eclipse uses to launch the same program?

3
How are you launching it from the command line? java -jar myjar or just plain java myclass? - Michael Myers
What platform are you running on? - cheeken
Have you looked at the list of dependencies in your Eclipse project? You should specify the same list of dependencies when calling your java program on the command line. - Laf
You need the same dependencies on the command line you have defined in your project's build path settings (minus compile-time-only requirements). - Dave Newton

3 Answers

14
votes

It looks like your classpath isn't set appropriately when you try to run via the command line. You can easily export the command Eclipse uses by doing this:

  1. Run your application and go to your Debug perspective
  2. In the process viewer, there should be an entry for the app you've just executed
  3. Right-click the row that mentions java.exe or javaw.exe
  4. Select Properties
  5. In the dialog that pops up you'll see the Command Line which includes all classpath entries and arguments
1
votes

The classpath that Eclipse will use should be visible in your project's properties dialog...

Or, on a Unix-like system, you can cheat, with something like (Linux/Fedora) ps axww | grep java to peek around for the actual command-line.

On Linux, specifically, /proc/pid/cmdline will give the entire command-line (split with '\0' chars, so it may look run-together if you just cat the file) and /proc/pid/environ will show environment variables, if you need to peek at them, as well. (e.g. CLASSPATH environment variable)

1
votes

You're asking 2 different questions here.

You can check your Java project's classpath by right clicking on the project name, and left clicking on the Properties option at the bottom.

On the Properties dialog, left clicking on the Java Build Path brings up the build path dialog.

If you want to determine the Eclipse command line string, you look at the Run Configurations. On the menu, left click on Run, then left click on Run Configurations. Under Java Application, you should find the run configuration for your project. The Arguments tab shows you how Eclipse starts your Java project.