0

I have successfully compiled this file and saved.

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

using

javac a.java
java A

but when I compile this file:

package B;

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

now, again using the same commands it do compile but never run

javac a.java
java A       
// could not find or load main class

Please guide me the exact command for the terminal to run the file.

Note: The file is named "a.java".

2
  • java B.A - you should specify package Commented Mar 6, 2015 at 12:08
  • might be helpful - package and folder names Commented Mar 6, 2015 at 13:04

3 Answers 3

2

You need to specify the fully-qualified name, i.e. packageName.ClassName:

java B.A
Sign up to request clarification or add additional context in comments.

Comments

1

you have to change the directory to the directory wich contains the path 'B' (your package name) and than call java B.A

8 Comments

... and A.java must be saved in the path ./B/A.java
No, javac doesn't require the package name. java does.
I went to the directory, A.class is in the same location with a.java, thus again I am having the same error @Axel
@RohanChauhan you mist be in the directory that contans directory B and inside B must be A.class.
@Jens I dont have any directory inside my "java" directory in which I saved the a.java file. even when I use "cd B" its says, directory does not exist.
|
-1

As mentioned above, You need to specify the fully-qualified name, i.e. packageName.ClassName:

        >> javac a.java
        >> java B.A

But you need to create the directory named "packageName" yourself as jdk does not create one for you implicitly.

1 Comment

with javac -d it does - see javac manual

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.