134

I have a compiled java class:

Echo.class

public class Echo {
    public static void main (String arg) {

            System.out.println(arg);
    }
}

I cd to the directory and enter: java Echo "hello"

I get this error:

C:\Documents and Settings\joe\My Documents\projects\Misc\bin>java Echo "hello"
Exception in thread "main" java.lang.NoClassDefFoundError: Echo
Caused by: java.lang.ClassNotFoundException: Echo
        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)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Echo.  Program will exit.

What is the simplest way to get my java code in a form that I can run from the command line as apposed to having to use Eclipse IDE?

Mel
  • 5,837
  • 10
  • 37
  • 42
joe
  • 16,988
  • 36
  • 94
  • 131
  • 18
    Suggestion: The original question would, perhaps, better be left unedited in order not to invalidate the answers, under a quick review. Really weird to see the right method signature and then a bunch of answers that say it was incorrect. Erroneous code is supposed to be left that, for the purpose of comparison with the correct version, at least. – JSmyth Feb 03 '14 at 09:34

7 Answers7

134

Try:

java -cp . Echo "hello"

Assuming that you compiled with:

javac Echo.java 

Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )

If that's the case and listing the contents of your dir displays:

Echo.java
Echo.class

Then any of this may work:

java -cp . Echo "hello"

or

SET CLASSPATH=%CLASSPATH;.  

java Echo "hello"

And later as Fredrik points out you'll get another error message like.

Exception in thread "main" java.lang.NoSuchMethodError: main

When that happens, go and read his answer :)

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 2
    The -cp . to tell Java that he should add . (current working directory) to the classpath, meaning the place where he is looking for code. – Fredrik Aug 14 '09 at 19:24
40

With Java 11 you won't have to go through this rigmarole anymore!

Instead, you can do this:

> java MyApp.java

You don't have to compile beforehand, as it's all done in one step.

You can get the Java 11 JDK here: JDK 11 GA Release

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
  • > copy C:\Desenv\workspaceServer\TestProject\src\WebServiceStandAlone\MyApp.java . /y && javac MyApp.java && java -cp . MyApp && del MyApp.class && del MyApp.java Some guys do prefer the old fashion way =) – Jr. Nov 13 '18 at 20:15
20

You need to specify the classpath. This should do it:

java -cp . Echo "hello"

This tells java to use . (the current directory) as its classpath, i.e. the place where it looks for classes. Note than when you use packages, the classpath has to contain the root directory, not the package subdirectories. e.g. if your class is my.package.Echo and the .class file is bin/my/package/Echo.class, the correct classpath directory is bin.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
18

You have no valid main method... The signature should be: public static void main(String[] args);

Hence, in your case the code should look like this:

public class Echo {
    public static void main (String[] arg) {

            System.out.println(arg[0]);
    }
}

Edit: Please note that Oscar is also right in that you are missing . in your classpath, you would run into the problem I solve after you have dealt with that error.

Community
  • 1
  • 1
Fredrik
  • 5,759
  • 2
  • 26
  • 32
6

If you have in your java source

package mypackage;

and your class is hello.java with

public class hello {

and in that hello.java you have

 public static void main(String[] args) {

Then (after compilation) changeDir (cd) to the directory where your hello.class is. Then

java -cp . mypackage.hello

Mind the current directory and the package name before the class name. It works for my on linux mint and i hope on the other os's also

Thanks Stack overflow for a wealth of info.

Gerard Doets
  • 61
  • 1
  • 2
  • 3
    NB: the class file should be foundable via `./mypackage/hello.class` name. See also: "[Running java in package from command line](https://stackoverflow.com/questions/19433366/running-java-in-package-from-command-line)" question. – ruvim Apr 24 '18 at 11:40
3

My situation was a little complicated. I had to do three steps since I was using a .dll in the resources directory, for JNI code. My files were

S:\Accessibility\tools\src\main\resources\dlls\HelloWorld.dll
S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.class

My code contained the following line

System.load(HelloWorld.class.getResource("/dlls/HelloWorld.dll").getPath());

First, I had to move to the classpath directory

cd /D "S:\Accessibility\tools\src\test\java"

Next, I had to change the classpath to point to the current directory so that my class would be loaded and I had to change the classpath to point to he resources directory so my dll would be loaded.

set classpath=%classpath%;.;..\..\..\src\main\resources; 

Then, I had to run java using the classname.

java com.accessibility.HelloWorld 
Scott Izu
  • 2,229
  • 25
  • 12
0

First, have you compiled the class using the command line javac compiler? Second, it seems that your main method has an incorrect signature - it should be taking in an array of String objects, rather than just one:

public static void main(String[] args){

Once you've changed your code to take in an array of String objects, then you need to make sure that you're printing an element of the array, rather than array itself:

System.out.println(args[0])

If you want to print the whole list of command line arguments, you'd need to use a loop, e.g.

for(int i = 0; i < args.length; i++){
    System.out.print(args[i]);
}
System.out.println();
quanticle
  • 4,872
  • 6
  • 32
  • 42