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.

Sabien
  • 219
  • 2
  • 16
  • Your first class has an extra '}' in it. Remove the extra '}' and try again. – Fuzzy Analysis Oct 11 '14 at 00:00
  • Also, see if there is a classpath file that points to the class references being somewhere else. – Fuzzy Analysis Oct 11 '14 at 00: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. – Sabien Oct 11 '14 at 00:04
  • try using the -classpath or -cp option as outlined here: http://stackoverflow.com/questions/2864622/how-do-i-run-class-files-on-windows-from-command-line – Fuzzy Analysis Oct 11 '14 at 00:06

1 Answers1

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
sethmuss
  • 169
  • 9
  • 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 Oct 11 '14 at 00:13
  • @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. – user207421 Oct 11 '14 at 00:39
  • 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. – Sabien Oct 11 '14 at 01:04
  • 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 (http://docs.oracle.com/javase/tutorial/java/package/). But it can be confusing until you get used to it. – sethmuss Oct 11 '14 at 01:57