Skip to main content

I'm new and need help for my first java program (updated)

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
3 replies [Last post]
2kjava
Offline
Joined: 2012-04-18

I'm new to Java and am trying to compile and run my first program using the java development sdk. Although it complies to .class OK, running it using java produces run time errors. Please would some kind person tell me what's wrong as I'm used to using Microsoft IDE for j# and c++.

Also can you recommend a free IDE for java please?

The java program is
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}

This produces

C:\Program Files\Java\jdk1.6.0_31\bin>java HelloWorldApp
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class
Caused by: java.lang.ClassNotFoundException: HelloWorldApp
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: HelloWorldApp. Program will exit.

Any help appreciated!

[update]
I've now installed and have working NetBeans 7.1. This allows me to compile and run my java programs. The jar files produced also work at the command prompt using java -jar ... So I know that I have a working java runtime and development environment.

The problem I experience is if I try to use java command with a .class file containing a main method - even those produced by NetBeans still give the same error as above.

Please would someone explain how to run .class files with the java command.

John.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
egc2224
Offline
Joined: 2006-02-17

i would recommend Eclipse. try running your code inside it. I think you are just not finding your class file when you try to run it using the command prompt.

2kjava
Offline
Joined: 2012-04-18

I've now using NetBeans which works OK.

The question, though, is why from the command prompt java is not finding the class file/main entry point?

NetBeans produces a jar file which works fine from the command prompt with java -jar - but the same problem is still present using the class file produced from NetBeans as with using class file produced by javac.

John.

suryahotha
Offline
Joined: 2012-05-04

As you are using IDE's other than eclipse & Netbeans, just add the dot(.;) at begining of CLASSPATH
environment variable. [.;%JAVA_HOME%\jre\lib\rt.jar;%JAVA_HOME%\lib\tools.jar]. Hope this helps you.

Surya Hota