Solved

Cannot compile JAVA program on Windows 7 PC

Posted on 2014-10-22
7
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

724 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