Sabien
Sabien

Reputation: 219

java command will not execute my .class file

I have two .class files that are in the same directory.

One is a class file containing a class I wrote that does not have a main function. The other class file contains just the public static void main function that creates an object of my class and calls one function.

When I compile and run these within Netbeans IDE, it runs fine. If I navigate to the .class files through the Windows Command prompt and try to run the files using the java command, I get an error saying it cain't find the main class.

Here's my class with the main function:

package a3;
public class mainTest
{
    public static void main(String[] args)
    {
        A3 test = new A3();

          test.quiz();
    }
}

And my class with all of my methods is defined like so:

package a3;

import java.util.Scanner;
import java.util.Random;

public class A3
{

    public void quiz()
    {
        // stuff
    }

    //more helper functions called from quiz function

} // end of class

When I try to run from the command prompt using: java mainTest

I get: Error: could not find or load main class mainTest even though I'm staring at the mainTest.class file in the directory from which I'm using that command... What am I missing here?

Also I should not that I'm able to launch other java applications with the same command, so I don't think it has anything to do with the environment variable. It must be something with my code.

Upvotes: 0

Views: 2331

Answers (1)

sethmuss
sethmuss

Reputation: 179

You need to run it from the directory outside of the a3 directory (the one that has the class files), execute this:

java a3.mainTest

Upvotes: 3

Related Questions