0
package p1;

class a {
    public static void main(String[] argument) {
        //nothing
    }
}

And when I type java p1.a in command prompt, it always say Main class not found? Why so?

Can any one help me out? Why this is happening? Is the syntax correct?

stealthjong
  • 10,858
  • 13
  • 45
  • 84
user3380123
  • 703
  • 1
  • 6
  • 14
  • 2
    how did you compile it and where does the p1 folder reside – SparkOn Jul 03 '14 at 13:26
  • it was on the desktop,and i complied after changing the directory to the folder,and then java p1.a – user3380123 Jul 03 '14 at 13:27
  • If it is desktop first open the cmd in desktop then compile it by javac p1/a.java and run it by java p1.a remember dont go inside the folder p1 and try the commands – SparkOn Jul 03 '14 at 13:29
  • You actually don't specify the package name. See this link for more details. [http://www.oracle.com/technetwork/java/compile-136656.html#comp] – dARKpRINCE Jul 03 '14 at 13:29
  • @Jonk it doesnot matter as long as the file contains only one class – SparkOn Jul 03 '14 at 13:33
  • 1
    @M.Sharma Yeah, as I mentioned I was off to read the JLS to find out for certain, and you're right. – JonK Jul 03 '14 at 13:35

3 Answers3

1

you need to do javac first.. javac generates a .class file. And try using the full (absolute) path when using java command.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1

Read this

When you create a package, you have to compile the package with -d option in javac. Then try executing the main class.

Community
  • 1
  • 1
user3717646
  • 436
  • 3
  • 10
1

First as there is a package p1; you need to create the folder p1 and put the file inside it then as you said if you folder p1 is in desktop, then open the cmd in desktop then compile it by javac p1/a.java and run it by java p1.a

SparkOn
  • 8,806
  • 4
  • 29
  • 34