PDA

View Full Version : java not executing after compilation?


abhilashm86
June 12th, 2009, 01:41 AM
what is basic error in this? i did this in vim and saved as j1.java


class myapp {
public static void main(String args[]) {
System.out.println("i code java");
}
}



abhilash@abhilash:~$ javac j1.java
abhilash@abhilash:~$ java j1
Exception in thread "main" java.lang.NoClassDefFoundError: j1
Caused by: java.lang.ClassNotFoundException: j1
at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)


why is not executing,

Linteg
June 12th, 2009, 03:15 AM
Because the file doesnt exist, javac creates classname.class files, try running java myapp

jespdj
June 12th, 2009, 05:33 AM
First of all, you have to make your class myapp public:
public class myapp {
// ...
Second, Java requires that source file names are named after the public class they contain. So your file must be named myapp.java, not j1.java. Rename your source file and then compile and run it like this:
javac myapp.java
java myapp

See Sun's Hello World tutorial (http://java.sun.com/docs/books/tutorial/getStarted/cupojava/unix.html).

rocketflame
June 12th, 2009, 08:02 AM
class names need to start with a CAPITAL

myapp should be

Myapp

you should also save the file with the class name, so the file name should be

Myapp.java

Zugzwang
June 12th, 2009, 08:35 AM
class names need to start with a CAPITAL


This is just a part of the Java code conventions, but not a strict requirement. The equivalence of the class name and the .java file name is however a requirement.