Command Prompt cannot find class in my Java program

Closed
zlloyd1 Posts 10 Registration date Tuesday 11 December 2012 Status Member Last seen 4 February 2013 - 11 Dec 2012 à 00:38
shoaibista Posts 8 Registration date Friday 24 May 2013 Status Member Last seen 27 May 2013 - 24 May 2013 à 11:10
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!!

Related:

2 responses

rizvisa1 Posts 4478 Registration date Thursday 28 January 2010 Status Contributor Last seen 5 May 2022 766
12 Dec 2012 à 18:43
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....
rizvisa1 Posts 4478 Registration date Thursday 28 January 2010 Status Contributor Last seen 5 May 2022 766
13 Dec 2012 à 12:37
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");}} 
shoaibista Posts 8 Registration date Friday 24 May 2013 Status Member Last seen 27 May 2013
24 May 2013 à 11:10
->javac guihelloworld.GUIHelloWorld


try this