0

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.

4
  • Your first class has an extra '}' in it. Remove the extra '}' and try again. Commented Oct 11, 2014 at 0:00
  • Also, see if there is a classpath file that points to the class references being somewhere else. Commented Oct 11, 2014 at 0:03
  • I'm not sure how to find this. There is a project.properties file within my Netbeans project, and I see some code within this file referring to classpath, but there are many different lines related to classpath - I'm not sure what any of these mean. Commented Oct 11, 2014 at 0:04
  • try using the -classpath or -cp option as outlined here: stackoverflow.com/questions/2864622/… Commented Oct 11, 2014 at 0:06

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

This worked, thanks! Is it because I have the package a3; code at the start? If I delete this line of code, will I be able to simply run it from its directory? Netbeans put this in by default.
@Sabien No, you defined a package called a3 to NetBeans and put this class inside it. If you don't want it in a package, just move it to the default package, and NetBeans will fix the source. But classes should be in packages.
But I never wrote that line of code, package a3; - it was there when I created the project. I'm not sure where it came from. Regardless, now I know.
Most IDEs (like netbeans) want you to put your Java classes into a package, and they are a good practice to keep your project organized (docs.oracle.com/javase/tutorial/java/package). But it can be confusing until you get used to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.