Petrus K.
Petrus K.

Reputation: 840

running .class files with java.exe - Error: Could not find or load main class

code:

package pack1;

public class Demo01 {

public void run() {
    System.out.println("--running Demo01--");
    demoMethod1();
}

private void demoMethod1() {
    int foo = 5;
    int bar = 10;

    int res = foo+bar;
    System.out.println("res: "+res);
}

public static void main(String[] args){
    Demo01 demo01 = new Demo01();
    demo01.run();
    // new change...

    Demo02 demo02 = new Demo02();
    demo02.run();
    }
}

rest can be found here: https://code.google.com/p/ci-research-teamcity-test-project/source/browse/#svn%2Ftrunk%2Fsrc%2Fpack1

I'm trying to run the .class files with java.exe through the command line to no avail. Yes I've looked for solutions, tried running the root folder with the -cp flag but I keep getting the same error. Works just fine in Eclipse.

Upvotes: 1

Views: 749

Answers (1)

MrTux
MrTux

Reputation: 34002

Ok, we have several points to note at this point.

The class is in a package. Thus, it must be in a folder names exactly as the package name ("pack1" in your case).

Your folder structure must be like this:

"root folder" (X)
| pack1
  | Demo01.class
  | Demo02.class (as I just noticed that you are also referring to it in the code)

Then, in order to start it, you have to be in the parent folder (this has to be the current working directory; marked with X) of "pack1" and execute

java pack.Demo01

Note, you have to use the whole canonical class name for referencing it, without .class at the end.

If you don't want or cant change the current working directory to the "root folder" you can use -cp PATH as first parameters to java.exe.

Upvotes: 2

Related Questions