How to execute java.class from the command line

I have a compiled java class:

Echo.class

public class Echo { public static void main (String arg) { System.out.println(arg); } } 

I cd into the directory and type: 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 easiest way to get my Java code in a form that I can run from the command line, since it is related to the need to use the Eclipse IDE?

+69
java
Aug 14 '09 at 18:52
source share
6 answers

Try:

 java -cp . Echo "hello" 



Assuming you are compiled with:

 javac Echo.java 

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

If this event and the list of contents of your directory shows:

 Echo.java Echo.class 

Then any of this can work:

 java -cp . Echo "hello" 

or

 SET CLASSPATH=%CLASSPATH;. java Echo "hello" 

And later, like Fredrik , you will get another error message.

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

When this happens, go and read his answer :)

+81
Aug 14 '09 at 18:53
source share

You need to specify the path to the classes. This should do it:

 java -cp . Echo "hello" 

This suggests what java is using . (current directory) as its path to classes, that is, the place where it searches for classes. Note that when using packages, the class path must contain the root directory, not the package subdirectories. for example, if your class is my.package.Echo and the .class file is bin/my/package/Echo.class , then the correct path directory for the bin path is.

+11
Aug 14 '09 at 18:54
source share

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

Therefore, 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 the Oscars are also right that you are absent. on your way to classes, you are faced with a problem that I solve after you deal with this error.

+11
Aug 14 '09 at 18:54
source share

There may be a few mistakes - here is a tutorial that should get you started: Starting Java as a command-line application .

+5
Aug 14 '09 at 18:55
source share

My situation was a bit complicated. I had to take three steps since I used .dll in the resource 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 route directory

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

Then I had to change the class path to point to the current directory so that my class was loaded, and I had to change the class path to point to its resource directory so that my DLL was loaded.

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

Then I had to run java using the class name.

 java com.accessibility.HelloWorld 
+2
Aug 10 '13 at 1:30
source share

First, did you compile the class using the javac command-line compiler? Secondly, it seems that your main method has an incorrect signature - it should accept String objects in the array of objects, and not just one:

 public static void main(String[] args){ 

After you change the code to take an array of String objects, you need to make sure that you are printing an element of the array, not an array:

 System.out.println(args[0]) 

If you want to print the entire list of command line arguments, you will need to use a loop like

 for(int i = 0; i < args.length; i++){ System.out.print(args[i]); } System.out.println(); 
0
Aug 14 '09 at 19:03
source share



All Articles