tags:

views:

184

answers:

5

I have a compiled java class:

Echo.class

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

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

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.

Thanks!

+5  A: 

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 :)

OscarRyz
+1 as promised..
Fredrik
that seems to work but why the: java -cp .
joe
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
Thinking about it, I think java is a she...
Fredrik
+1  A: 

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
+4  A: 

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

Hence, in your case the code would 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.

Fredrik
+1 for reading the code
Allyn
@Allyn: I would -1 for not reading the exception though ::) :) :) But we are among friend here don't we?
OscarRyz
@Oscar: I gave you credits for it, didn't I :-) (actually, I am going to give you +1 for it as well)
Fredrik
@Fredrik. Don't you love StackOverflow??? here +1 ;)
OscarRyz
@Oscar: Love is not a strong enough word, I don't know understand how I managed to survive before there was a StackOverflow :-D
Fredrik
+3  A: 

There could be several things wrong - here is a tutorial that should get you started: Running Java as a Command Line Application.

Andrew Hare
A: 

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