Babak Mehrabi
Babak Mehrabi

Reputation: 2275

problem on executing a java class file

I am new to java. I write a simple code like this:

import java.io.*;


public class a
{
    public static void main(String []argc)
    {
        System.out.println("S");
    }
}

I compile it with below bash command:

javac a.java

then this:

java a

But it said:

Could not find or load main class a

My java version is 1.6.0. What should I do?

Upvotes: 2

Views: 260

Answers (3)

Saket
Saket

Reputation: 46157

Use this to run :

java -cp . a

Basically, you need to add the directory where your compiled .class file is to your classpath (which is the current directory, ., in your case).

Also, your code at this time uses no other APIs from external libraries, but most likely you would going forward. In that case, ensure that you add those JARs to you classpath (using java -cp .;<jar1 path>;<jar2 path> a) when running your code.

Upvotes: 4

Andrey Atapin
Andrey Atapin

Reputation: 7955

You need to specify classpath containing current directory:

java -cp ./ a

Upvotes: 1

Joachim Sauer
Joachim Sauer

Reputation: 308269

A common reason for this is that you've set the environment variable CLASSPATH.

This is usually not a good idea, because that setting always influences your whole system.

You can easily define a per-instance classpath, by specifying the -cp parameter.

In your case you can do

java -cp . a

This tells Java to look for classes in the current directory (.).

Upvotes: 7

Related Questions