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".

harshtuna
  • 757
  • 5
  • 14
  • `java B.A` - you should specify package – Maxim Mar 06 '15 at 12:08
  • might be helpful - [package and folder names](http://stackoverflow.com/questions/8395916/package-name-is-different-than-the-folder-structure-but-still-java-code-compiles) – harshtuna Mar 06 '15 at 13:04

3 Answers3

2

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

java B.A
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

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

Jens
  • 67,715
  • 15
  • 98
  • 113
-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.

  • with `javac -d` it does - see [javac manual](http://docs.oracle.com/javase/8/docs/technotes/tools/unix/javac.html) – harshtuna Mar 06 '15 at 13:07