I'm a bit puzzled by this. The code is about as simple as it gets:
public class MyFirstApp { public static void main (String[] args) { System.out.println("I rule!"); System.out.println("The World"); } }
I'd appreciate some assistance.
TIA
Steven E. Davies<br /> <br />"Those are my principles, and if you don't like them... well, I have others." - Groucho Marx
pascal betz
Ranch Hand
Joined: Jun 19, 2001
Posts: 547
posted
0
did you compile the class (running the java compiler using javac or in your IDE) ? is the class file on the classpath ?
because the error message just means that the class you want to run can not be found on the classpath.
the classpath is something like a collection of classes (.class files, but also class files in jar files) and resources (e.g. images you can load without using system specific/absolute file paths). you specify it using the -cp or -classpath option.
e.g. java -cp .;somedir/classes;somejar.jar MyApp
for simple programs (consisting of just the classes, no libraries that are used) it is usualy enough to change to the directory where the class files reside (root of your package structure).
pascal
Yogendra Joshi
Ranch Hand
Joined: Apr 04, 2006
Posts: 206
posted
0
Hey Steve ,
Welcome to the world of Java.. :-) Hey Bert , A new Novice Added to our Ranch..
Steve , What you can try to figure out is , Have you set the neccesary things to run your first app.. I mean Have you defined the path for your application ? Path variable is very important. What i would suggest you is this.
1)Add path variable to your environmental variables of your machine , If you dont know where they are , Here's it. Point ur mouse to My Computer -> Right Click -> Properties-> Goto Advanced Tab-> Environmental Variables.
2)Click on New under "User Variables" and give the name of the variable as path and value as the Address of the place where you installed j2sdk. For instance , I have it on my c: , So example can be variable name=path, variable value=c:\j2sdk1.4.0\bin, AFter setting this , Save your program in c:\j2sdk1.4.0\bin and then try to run your app again.. i.e save it as c:\j2sdk1.4.0\bin\MyFirstApp.
Have a great day ! Yogendra.
Meri Zindagi Hain Tab Tak.. Jab Tak Tera Sahara.... Har Taraf Tu Hi Tu Hain SAI Tera Hi Hain Nazara.....
Steve Davies
Greenhorn
Joined: Apr 14, 2006
Posts: 6
posted
0
I had the PATH EnVar set to the BIN directory. I had no problems compiling.
I'm confused with teh class settings. I did the typical install of JDK 1.5. The CLASSPATH is set to as follows:
Since I did not set it, it must have been done at install. I am creating a very simple program. The compile placed the .class file in the same directory as the .java file.
I suspect once I get this resolved I'll be off and running. Something just is clicking for me.
After compiling, have you confirmed that you actually have a class file (MyFirstApp.class) in the directory? If it's compiling okay, then your PATH variable should be correct.
When running, make sure that you're in the proper directory. The easiest way to do this is to change directories (using the command cd) to where your files are located. For example...
cd c:\myFiles\java javac MyFirstApp.java java MyFirstApp
NOTE: You should not need to change any CLASSPATH settings at this point. [ April 14, 2006: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Steve Davies
Greenhorn
Joined: Apr 14, 2006
Posts: 6
posted
0
The class file is in the same directory as the source code.
Directory of C:\Documents and Settings\xxxxxx\My Documents\My Programming\Java\Tutorials\Head First Java
When you change to that directory (using the cd command), then your prompt should not be just...
C:\>
At the prompt, type cd followed by the directory path...
C:\>cd C:\Documents and Settings\xxxxxx\My Documents\My Programming\Java\Tutorials\Head First Java
This should change your prompt to...
C:\Documents and Settings\xxxxxx\My Documents\My Programming\Java\Tutorials\Head First Java>
Then try to run...
C:\Documents and Settings\xxxxxx\My Documents\My Programming\Java\Tutorials\Head First Java>java MyFirstApp [ April 14, 2006: Message edited by: marc weber ]
Steve Davies
Greenhorn
Joined: Apr 14, 2006
Posts: 6
posted
0
I'm sorry. I was trying to make it more readable. The prompt can be whatever you wish it to be.
For instance:
prompt $N$G
Will get you:
C>
A prompt of:
C:\Documents and Settings\Steven\My Documents\My Programming\Java\Tutorials\Head First Java>
... is just too much information. When I need the path info, I just type
that tells java to use the current directory as (part of?) the classpath.
this is a short term solution, but it might at least let you get the code to run.
Never ascribe to malice that which can be adequately explained by stupidity.
Steve Davies
Greenhorn
Joined: Apr 14, 2006
Posts: 6
posted
0
Thanks, Fred. I tried it and received the following:
C>java -cp . MyFirstApp Exception in thread "main" java.lang.UnsupportedClassVersionError: MyFirstApp (Unsupported major.minor version 49.0) at java.lang.ClassLoader.defineClass0(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) 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)
Maybe I should have stuck with .Net (Just kidding!)
I appreciate the help. Thanks all.
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
You have compiled the source with a version 1.5 compiler, and now you are attempting to run the source with some lesser version.
You can verify this with: javac -J-version (you will see 1.5.x) java -version (you will see 1.y.z where y < 5)