Solved

Cannot compile JAVA program on Windows 7 PC

Posted on 2014-10-22
7
322 Views
Last Modified: 2014-10-27
When I compile the above program via command prompt: javac HelloWorld.java
The program compiles and I see HelloWorld.class file.
Now when I execute the command on command prompt: java HelloWorld
I get error:
Error: Could not find or load main class HelloWorld

I have installed JDK at the location C:\Program Files\Java\jdk1.8.0_25
and the environment variable JAVA_HOME has been set as C:\Program Files\Java\jdk1.8.0_25


----HelloWorld.java-----------
public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello, World");
    }

}
0
Comment
Question by:toooki
  • 4
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 40398265
Try

java -cp . HelloWorld

Open in new window

0
 

Author Comment

by:toooki
ID: 40398299
It works, thank you.
Could you kindly let me know how to avoid this "-cp ." every time.
Before I could run using java HelloWorld
Somehow after upgrading to the next Java version, some setup probably got changed.
I added JAVA_HOME and CLASSPATH environment variables. Is there anything else that needs to be setup?

Thank you.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40398302
Yes, i would recommend NOT setting CLASSPATH anything other than dynamically
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Expert Comment

by:rrz
ID: 40399828
If you have a CLASSPATH environment variable defined, then the VM uses the paths listed in the CLASSPATH environment variable rather than searching using its default paths. As a result, if you set this environment variable you need to add the current path otherwise the VM will not find classes in the current working directory.
from http://www.cs.swarthmore.edu/~newhall/unixhelp/debuggingtips_Java.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40399854
Yes. But even if you do include the current directory in the CLASSPATH you can often get nasty results. Fixed classpaths tend to be write once and forget.
0
 

Author Comment

by:toooki
ID: 40405721
Thank you!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40405917
:)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
going to wrong jsp page 2 33
swing controls 2 16
hashmap order 17 36
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question