Solved

Cannot compile JAVA program on Windows 7 PC

Posted on 2014-10-22
7
334 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

737 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