Tyler Tyler - 1 year ago 96
Java Question

Java - Package preventing java from finding/loading main method

I have this program with 3 source files, being pretty new to java I recently learned that I should package these. So I went and did that, I already knew a little about packaging. However, after trying to run the class file with the main method, java could not find/load the main method.

After a while, I finally discovered that the cause was the package line. When the package line is there, the error appears, when the package line isn't there (or is commented out), the program runs fine.

package PeriodicTable;

class PeriodicTable {
public static void main (String[] args) {
//Lines of code

According to various tutorials and the java doc, all you need to do is put the package line, the package name and a semi-colon at the end. Google searching the error (with package as the cause) did not help me.

I've tried changing the package name, so it was not the same as the class name, this did not work.

What am I using?


Command Prompt

Java 8

As for my question... Why is the package line preventing java from finding/loading the main method? How do I fix this?

Answer Source

Go to the package directory, in your case it is PeriodicTable. Run following commands

$ javac -cp . PeriodicTable/PeriodicTable.java 
$ java -cp .  PeriodicTable.PeriodicTable

Following link may help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download