Unfathomably Easy Java Question

noobman

[H]ard|Gawd
Joined
Oct 15, 2005
Messages
1,475
Hey,

I'm trying to create a ridiculously simple class to recreate an error I'm having with some larger software.

Code:
class TestThisOS
{
    public static void main(String args[]) 
    {
	String whatIsThisOS = System.getProperty("os.name");
        System.out.println("OS: " + whatIsThisOS);
    }
}
The file is called TestThisOS.java
When I run "javac TestThisOS.java", the file compiles to a class successfully.
However, when I attempt to run the class from the command line, I receive the following error:

Code:
Exception in thread "main" java.lang.NoCLassDefFoundError: TestThisOS.class
Caused by: java.lang.ClassNotFoundException: TestThisOS.class

...bla bla bla
Any ideas? I find it odd, because my %PATH% environment variable in Windows is configured and working.
 

devman

2[H]4U
Joined
Dec 3, 2005
Messages
2,400
What package is the class in and what is the command line you are using to run it.
 

djBon2112

Supreme [H]ardness
Joined
Jul 29, 2006
Messages
5,279
It's not a public class. If it's got a main method that you want to run from scratch, it's gotta be a public class. NoClassDefFoundError means it can't find the right class.
 

devman

2[H]4U
Joined
Dec 3, 2005
Messages
2,400
It's not a public class. If it's got a main method that you want to run from scratch, it's gotta be a public class. NoClassDefFoundError means it can't find the right class.

That is incorrect. You directly execute a default access class if it has a main method. It certainly isn't best practices though.
 

fluxion

Gawd
Joined
May 31, 2005
Messages
864
lol, i know what you're doing wrong. classic mistake:

Code:
fluxion@purity:~/temp$ cat TestThisOS.java 
class TestThisOS
{
    public static void main(String args[]) 
    {
		String whatIsThisOS = System.getProperty("os.name");
        System.out.println("OS: " + whatIsThisOS);
   }
}
fluxion@purity:~/temp$ javac TestThisOS.java 
fluxion@purity:~/temp$ java TestThisOS 
OS: Linux
fluxion@purity:~/temp$ java TestThisOS.class
Exception in thread "main" java.lang.NoClassDefFoundError: TestThisOS/class
Caused by: java.lang.ClassNotFoundException: TestThisOS.class
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
fluxion@purity:~/temp$
 
Top