Solved

Cannot compile JAVA program on Windows 7 PC

Posted on 2014-10-22
7
308 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
Comment Utility
Try

java -cp . HelloWorld

Open in new window

0
 

Author Comment

by:toooki
Comment Utility
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
Comment Utility
Yes, i would recommend NOT setting CLASSPATH anything other than dynamically
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:rrz
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 …
This video teaches viewers about errors in exception handling.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now