I did a module in Java in college but we did it all using Eclipse. Now I’d like to go back and learn it but by using the command line.
I set up a folder on my desktop, wrote out my application and saved it as ‘MyFirstApp.java’. Here it is:
public class MyFirstApp {
public static void main (String[] args) {
System.out.print("Hello World!");
}
}
I opened my command line, cd’d into the appropriate directory and then compiled by typing “javac MyFirstApp.java”.
This creates a file in the folder called “MyFirstApp.class”.
However when I try to run this by typing “java MyFirstApp” I get an error:
Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp
Caused by: java.lang.ClassNotFoundException: MyFirstApp
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Share.class. Program will exit.
I also tried running by typing “java -cp.MyFirstApp” but then I get this error:
Unrecognised option: -cp.MyFirstApp
Could not create the Java Virtual Machine
Is this likely to be a problem with my environmental variables? I don’t really know much about all this stuff.
You should add a space between cp and class name: