Command Prompt cannot find class in my Java program

[Closed]
Report
Posts
10
Registration date
Tuesday December 11, 2012
Status
Member
Last seen
February 4, 2013
-
Posts
8
Registration date
Friday May 24, 2013
Status
Member
Last seen
May 27, 2013
-
Hello,
I have a simple graphical interface program that includes a message that reads "Hello World" in it. I know, not too original, but I am just getting started here with Java.
Anyway, this program compiles and runs flawlessly in NetBeans 7.2.1 IDE, and performs as expected. Unfortunately, I need it to run anywhere, and that means it must compile and run in the command prompt, but it won't because it complains it cannot find the main class in the file. Here is the code:
/ * HELLO WORLD Interface*/ 
package guihelloworld; 
/*Zachariah A Lloyd 
 * Hello World Graphical User Interface.... */ 
import javax.swing.JOptionPane; 
public class GUIHelloWorld { 
    public static void main(String[] args){ 
        JOptionPane.showMessageDialog(null, "HELLO WORLD");}} 

and as I said, this works perfectly in NetBeans, but is giving me grief when I try to run it from the command prompt. The exact error message I am getting is:
"Error: Could not find or load main class GUIHelloWorld.java"
but it compiles just fine in the command prompt....

Can anyone PLEASE tell me why I am getting that error??
THANKS IN ADVANCE!!

2 replies

Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
you class is not in the path. Java is unable to find the class
most common way would be that you change dir to the location of the file and then execute the program
The file is on my desktop, and the directory is pointing to C:>Users>myname>Desktop, and it is still claiming to not be able to find the main class,. As shown in my code that I posted previously, there is a main class included in my program:
public static void main(String[] args){
JOptionPane.showMessageDialog(null, "HELLO WORLD");}}

So, I cannot for the life of me figure out what is the problem resulting in this error....
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
It cannot be on desktop because you have this line
package guihelloworld;
So the class must reside in folder "guihelloworld"

Other issue with your code is
/ * HELLO WORLD Interface*/
there cannot be space between / and * for it to be a comment

try this. I have removed your package statement. So now if you cd to the location of the compiled class and run the
javac GUIHelloWorld
it would word

/**Zachariah A Lloyd 
 * Hello World Graphical User Interface.... */ 
import javax.swing.JOptionPane; 
public class GUIHelloWorld { 
    public static void main(String[] args){ 
        JOptionPane.showMessageDialog(null, "HELLO WORLD");}} 
Posts
8
Registration date
Friday May 24, 2013
Status
Member
Last seen
May 27, 2013

->javac guihelloworld.GUIHelloWorld


try this