Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java Could not find or load main class

Im using fedora 19. Content of HelloWorld.java :

    class HelloWorld {
        public static void main( String args[] ) {
            System.out.println( "Hello World!!" );
        }
    }

I can successfully compile it using

javac HelloWorld.java

But i cannot run it using

java HelloWorld

It gives the following error

Error: Could not find or load main class HelloWorld

But i can run it using

sudo java HelloWorld

What am I missing here???

like image 257
Pranav Chugh Avatar asked Jul 05 '13 06:07

Pranav Chugh


2 Answers

You are not setting a classpath that includes your compiled class! java can't find any classes if you don't tell it where to look.

Try java -cp . HelloWorld

Source here

Not sure why it works with sudo though. My* guess would be, that the CLASSPATH is set for the root user and not for the current user.

like image 140
Burkhard Avatar answered Oct 25 '22 21:10

Burkhard


Dear Pranav Chugh,

1- cmd - go the directory of located java file

run the following on cmd

2- javac HelloWorld.java 
3- java HelloWorld             ---- not not add .class

here you will get the result

like image 29
SAR Avatar answered Oct 25 '22 19:10

SAR