-1

I tried running this on unix with javac & java command but it does not work

public class CommandLine {
 public static void main( String[] args ) {
  for( int i = 0; i < args.length; i++ ) {
  System.out.println( args[ i ] );
  }
 }  
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Do you have any package definition in this java file? if yes how are you using java command? – Ganesh S Oct 25 '16 at 01:50
  • What is the detail error message. Does it say which class not found? – Razib Oct 25 '16 at 01:51
  • I always used and IDE to run my programs i'm not sure what you mean by defining a package – Kevin Chim Oct 25 '16 at 01:52
  • CommandLine.javaException in thread "main" java.lang.NoClassDefFoundError: CommandLine/java Caused by: java.lang.ClassNotFoundException: CommandLine.java at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at – Kevin Chim Oct 25 '16 at 01:54
  • sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class: CommandLine.java. Program will exit. – Kevin Chim Oct 25 '16 at 01:54
  • [This might help](http://stackoverflow.com/questions/16137713/how-to-run-a-java-program-from-the-command-line) You might not be running from the correct directory – Tanager4 Oct 25 '16 at 01:56

1 Answers1

1

When calling javac, list the .java file extension:

javac CommandLine.java

When calling java, do not list a file extension:

java CommandLine

The compiled class resides in the file CommandLine.class. So it would make sense for the command to run the program to be java CommandLine.class. However, java demands that the .class extension be dropped.

Daniel O
  • 336
  • 2
  • 3
  • 8