• Post Reply Bookmark Topic Watch Topic
  • New Topic

HelloWorld my first JAVA program doesn't work  RSS feed

 
Mike Gannon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to run my first HelloWorld program to test my JAVA installation. I'm new to programming so be gentle.
I have searched the forums, checked my PATH and CLASSPATH variables but cannot get my program to run.

Here is the program:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}

It compiles as expected and generates the HelloWorldApp.class file

When I try to run the program I get the following error.
C:\J21>java HelloWorldApp
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp
Caused by: java.lang.ClassNotFoundException: HelloWorldApp
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)
Could not find the main class: HelloWorldApp. Program will exit.

I have added CLASSPATH and PATH variables for the JDK
CLASSPATH = C:\Program Files\Java\jdk1.6.0_01\lib\tools.jar

Path = %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%%SystemRoot%%\system32;%%SystemRoot%%;%%SystemRoot%%\System32\Wbem;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Enterprise Vault\EVClient\;C:\Program Files\Java\jdk1.6.0_01\bin

Any help would be appreciated. i'm determined to get off the floor this time

Mike
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Before

did you run
 
Mike Gannon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I did, but I have resdolved the problem.
I added a ; to the end of my CLASSPATH variable and the runtime works.
Sorry for wasting your time. I can't believe that somehow I missed this little detail last year and it stopped me from learning JAVA.
I spent a whole week re-installing changing the PATH and CLASSPATH last time round, and spent two days doing it this time.

Thanks anyway

Mike
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid you have been given some bad advice if you are setting a classpath. Unless the classpath was set by another program (QuickTime [=QT] used to be ntorious for that sort of thing), you should not have a system classpath at all. There are various ways to set a path, and you can find one way in our FAQ, and a slightly different way in the Oracle installation instructions. You should test your path by issuing the following instructions at a command line/terminal and you get more or less the same results on Windows® and Linux:-
[campbell@xxxxx ~]$ java -version
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)
[campbell@xxxxx ~]$ javac -version
javac 1.7.0_40
Delete the classpath altogether, unless it contains QT or similar entries. If it contains QT or similar, add .; (DOS/Windows®) or .: (not DOS/Windows®) at the beginning of the classpath. A system classpath usually causes more harm than good.
 
Mike Gannon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie - Thanks for the tip. The runtime works, but being curious I deleted the CLASSPATH to see if it had any adverse affects. The program still runs without the Classpath so I'll do as you suggested.

Thanks,

Mike
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works like this: When you do not have the CLASSPATH environment variable set, Java sets the classpath to "." (the current directory). That's why it works when you do not have CLASSPATH set at all.

If you do set CLASSPATH, then Java will use that. If you want the current directory to be included in this case, you have to explicitly add it to CLASSPATH.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Gannon wrote:. . .- Thanks for the tip. . . .
You‘re welcome and well done. If you follow the instructions given, you should be able to alter your PATH in two minutes rather than two days. You may find your PATH needs to include [on a Windows® box] c:\Program Files\java\jdk1.7.0_40\bin rather that what it said in the Oracle site (the most recent update is _40). If you get a version (as my earlier post) of java which doesn't appear the same as javac, alter the PATH by moving the Java installation part of the PATH towards its beginning.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!