views:

106

answers:

3

Hi,

This is probably a stupid question, but how do I run a class file on windows 7? I usually create my own .java files and then use a basic IDE (with JDK6) to compile it to a class and run it automatically. My professor gave a .class file that we are supposed to play with extensively but I have no idea how to to run it here. Note that I cannot run it by typing:

java classname.class

because it contains gui stuff especially buttons and such. If I do the above I get the following error:

java.lang.NoClassDefFoundError: Test1/2/class
Caused by: java.lang.ClassNotFoundException: Test1.2.class
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Test1.2.class.  Program will exit.
Exception in thread "main" 

Any help would be highly appreciated. Thanks.

+5  A: 

When you run it, don't include the .class suffix:

java classname
Dean Harding
It's a little more complicated when you have package structure other than the default package. I assumed jNoob typed *java Test1.2.class*, which suggests something funky and against java conventions.
GregS
@GregS: that's a good point, yes.
Dean Harding
Hi guys, I renamed the Test1.2.class to Test.class and I entered the following: "java Test" and it still gave me the above errors. Any other suggestions?
jNoob
@jNoob: You can't just rename the .class file, you need to know the name of the actual java class inside the file. Does the class even have a `public static void main` method? What did your professor tell you to do with the file?
Dean Harding
Quote from professor: "It consists of Java bytecodes, so youshould be able to copy it anywhere and run it using the JVM."
jNoob
@jNoob: But that doesn't mean you can just say `java classname` and run it. Does it have a `public static void main` method?
Dean Harding
@codeka, I really don't know. It's kind of a puzzle that he wants us to figure out so he wouldn't tell us the source code because then we would obviously know the solution to it. You know what, I'll just ask him what to do.
jNoob
@jNoob: that's probably the best idea :)
Dean Harding
Hey, turns out the prof simply named the class a specific name that he forgot to tell us about. Everything worked out after I renamed the class. Thanks for everyones help though.
jNoob
+1  A: 

In addition to the above, it should also make no odds what your O/S is. Java compiles to byte code, which is interpreted by your JVM. You clearly have one installed since you got the java error you have pasted above.

Matt
+1  A: 

Like codeka said, you need to type java ClassName at the command line, and if the class has a main method, it will be run.

Java compiles each class that is defined in a particular source file into its own class (byte code) file. For example, Apple.class, Banana.class, and Cherry.class might get output after compiling Apple.java, if they are all defined there. So the actual name of the class in source will match the name of the file, minus the extension.

Now, let's say that someone accidentally (or intentionally, from the sound of it) renamed the class file. You have a file called WrongName.java, and you type java WrongName. Note that the output will begin with the line:

Exception in thread "main" java.lang.NoClassDefFoundError: WrongName (wrong name: RightName)

Where RightName is what it should be. At that point you would rename your file to RightName.class, type java RightName, and hopefully it will run. And if the name has a slash, then whatever precedes the slash is the name of the package. Let's say it's PackageName/RightName. First you need to create a directory called PackageName, and put RightName.class inside of it. Then, go one level up in your directories, and type java PackageName.RightName.

Note the different kinds of exceptions: ClassNotFoundException basically means that the class file was not found. NoClassDefFoundError means that the class definition was not found. If the class does not have a main method and you try to run it as a program, you will get NoSuchMethodError: main.

John C