aspose file tools*
The moose likes Beginning Java and the fly likes Cannot run program from another directory other than c:\Sun\SDK\jdk\bin Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot run program from another directory other than c:\Sun\SDK\jdk\bin" Watch "Cannot run program from another directory other than c:\Sun\SDK\jdk\bin" New topic
Author

Cannot run program from another directory other than c:\Sun\SDK\jdk\bin

James O'Reilly
Greenhorn

Joined: Jan 23, 2010
Posts: 9
I have a stupid brand new question.

I am teaching myself Java with the "Head First Java Book" by O'Reilly and am on day # 2 1/2. As ever - the 1st step is to make sure that I have a functioning version of Java SDK on my machine, so I can perform the excercises before I start reading too deeply into the text book. I downloaded java J2EE 5.00 from Sun Microsystems. I was told to include a "PATH" command in my "My Computer - Properties - Advanced - Environment Variables - User Variables to C:\Sun\SDK\bin which was already there after I installed Java SDK on my Windows XP box.

I created my 1st "MyFirstApp.java" file in notepad. I attempted to type "javac MyFirstApp.java" at the directory that I was in and got an error. I searched my computer and discovered that "javac" and "java" executable were in a directory "C:\Sun\SDK\jdk\bin", not "C:\Sun\SDK\bin"... If I typed "C:\Sun\SDK\jdk\bin\javac MyFirstApp.java" it would work (a.k.a. - the program would compile). It would also compile if I typed "PATH=C:\Sun\SDK\jdk\bin" and then typed the javac compile command, followed by thename of the program followed by the ".java" extension.

I adjusted the properties for the user (my user profile) to include "C:\Sun\SDK\jdk\bin" and rebooted. Nothing happened - I tried this a couple of times by adding the "\" at the end of "bin' and taking it out, as the case may be. I then added that parameter to the "Systems" Variables PATH command. When I rebooted (I hoped that my machine "would" come up as I thought that adding this parameter to the system path may be a bad thing and confuse the windows operating system upon boot up), I went to the black "C: command" prompt screen. I then typed "Javac MyFirstApp.java" from "C:\Sun\SDK" and it now works fine (it compiled without me having to make any further references to the "jdk\bin" directory . But When I attempt to run the program from C:\Sun\SDK by typing "java MyFirstApp" I get a string of errors (exceptions in thread main - and lines of No source found). I would expect that both command would either not work or work if I added the "C:\Sun\SDK\jdk\bin parameter to the system PATH command, but not just the javac compilation command and not the java. executional command???

Any answers to this dilemma would be extremely appreciated. Of course, if I type a "PATH C:\Sun\Sdk\jdk\bin" for the rest of my life at the command prompt all the commands will work from the directory that I am currently in, but I don't wish to do this!


Thanks In Advance

Jim O.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
James,

You're not posting in the correct forum. You question is on beginner Java, so that would be a better forum.

It sounds like you got the jdk/bin directory on your PATH correctly, but you are not telling javac where to find your source file. Try changing to the directory where your source file is located, then run javac with your source file name.

Note that it sounds like you're not very familiar with working in a shell (the command box, as you call it) and setting PATHs, etc. That is going to make learning Java hard, as you will be expected to know how to work within the environment. Perhaps a Windows for Beginners book would be a good idea? Someone else may have a better suggestion.

Good luck.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Mark E Hansen wrote:You're not posting in the correct forum. You question is on beginner Java, so that would be a better forum.


Thanks Mark, let me move it there.
James O'Reilly
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Paul Clapham wrote:
Mark E Hansen wrote:You're not posting in the correct forum. You question is on beginner Java, so that would be a better forum.


Thanks Mark, let me move it there.


Actually, the compilation works correctly and I am actually a 20 year programming veteran. I'm just a little confused as to why I can use the "javac" command and compile from a different directory than "C:\Sun\SDK\jdk\bin, but not execute the program as well. One cammand seems to work from a differnt directory than the above mentioned directory, but the other "java" command does not.'
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
James O'Reilly wrote:
Paul Clapham wrote:
Mark E Hansen wrote:You're not posting in the correct forum. You question is on beginner Java, so that would be a better forum.


Thanks Mark, let me move it there.


Actually, the compilation works correctly and I am actually a 20 year programming veteran. I'm just a little confused as to why I can use the "javac" command and compile from a different directory than "C:\Sun\SDK\jdk\bin, but not execute the program as well. One cammand seems to work from a differnt directory than the above mentioned directory, but the other "java" command does not.'

Then I don't understand what your problem could be. Do you know what the PATH environment variable is and how to set it? Do you understand the concept of the "current directory"? Based on your post, it sounds like you do not. If I'm mistaken, please accept my apologies and try re-stating your question.

Best Regards,
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

James O'Reilly wrote:... but not execute the program as well. One cammand seems to work from a differnt directory than the above mentioned directory, but the other "java" command does not.'

If you are running the "java" command from the directory where the class file resides and you don't have CLASSPATH defined it should work fine, by default it searches for the current directory for class files. But you can use "-cp" switch with java (i.e: "java -cp <PathToClassFile> <ClassFile>") to point to class files.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
liqin ren
Greenhorn

Joined: Jan 23, 2010
Posts: 2
[deleted]
liqin ren
Greenhorn

Joined: Jan 23, 2010
Posts: 2
[deleted]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

Hi liqin,
Please use English in the forums and Welcome to JavaRanch
James O'Reilly
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Vijitha Kumara wrote:
James O'Reilly wrote:... but not execute the program as well. One cammand seems to work from a differnt directory than the above mentioned directory, but the other "java" command does not.'

If you are running the "java" command from the directory where the class file resides and you don't have CLASSPATH defined it should work fine, by default it searches for the current directory for class files. But you can use "-cp" switch with java (i.e: "java -cp <PathToClassFile> <ClassFile>") to point to class files.


Thank you for getting back to me Mark. I did not set the classpath at all. I will try it later on to see if it works. The confusing thing is that I am sitting in the direrctory where the class file resides when I type" java MyFirstApp" and I get an error. However I can compile from this directory just fine with all in the java commands being in another direcoty. One command works (the compile command), but the other doesn't (the run command). I will investigate the class path command though. I guess that is what I get for "jumping ahead". I just wanted to make sure that I could compile and run java programs before I started really readin the java book. Thanks!

Jim O
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Vijitha Kumara wrote:Please use English . . .
Non-English text deleted.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
James O'Reilly wrote:Thank you for getting back to me Mark. I did not set the classpath at all. I will try it later on to see if it works. The confusing thing is that I am sitting in the direrctory where the class file resides when I type" java MyFirstApp" and I get an error. However I can compile from this directory just fine with all in the java commands being in another direcoty. One command works (the compile command), but the other doesn't (the run command). I will investigate the class path command though. I guess that is what I get for "jumping ahead". I just wanted to make sure that I could compile and run java programs before I started really readin the java book. Thanks!

Jim O

When you compile, the CLASSPATH has to be set so the compiler can find any classes which the source being compiled depends on. Note that the class path automatically includes the built-in Java classes. When you run the application, CLASSPATH needs to be set so the Java VM can find any classes that need to be loaded - even if they're in the current directory. I suspect that the book you're reading will cover this in better detail.
James O'Reilly
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Mark E Hansen wrote:
James O'Reilly wrote:Thank you for getting back to me Mark. I did not set the classpath at all. I will try it later on to see if it works. The confusing thing is that I am sitting in the direrctory where the class file resides when I type" java MyFirstApp" and I get an error. However I can compile from this directory just fine with all in the java commands being in another direcoty. One command works (the compile command), but the other doesn't (the run command). I will investigate the class path command though. I guess that is what I get for "jumping ahead". I just wanted to make sure that I could compile and run java programs before I started really readin the java book. Thanks!

Jim O

When you compile, the CLASSPATH has to be set so the compiler can find any classes which the source being compiled depends on. Note that the class path automatically includes the built-in Java classes. When you run the application, CLASSPATH needs to be set so the Java VM can find any classes that need to be loaded - even if they're in the current directory. I suspect that the book you're reading will cover this in better detail.


Than k you very much Mark!! I will try playing with that tommorow (or during the week) and definately let you know how it worked out. Today is Sunday and I'm going to something less challenging like play games of Facebook(LOL). Take care and thank you very much for the much appreciated help.. That has got to be the reason if the class path must be told where the "object" is (for lack of a better work like class is - even if it is in the same directory as the source..

Jim O
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

James O'Reilly wrote:when I type" java MyFirstApp" and I get an error.

it helps people help you if you tell us EXACTLY WHAT THE ERROR IS. Otherwise, folks are just guessing. are you getting a 'command not found' error? then it's a PATH issue or you don't have the JRE installed.

is it a 'class not found' erorr? then it could be a classpath issue.

Is it an exception at runtime? then it's something else. My point is that if you don't tell us exactly what the problem is, we're all just guessing, which is pretty much a waste of time.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
James O'Reilly
Greenhorn

Joined: Jan 23, 2010
Posts: 9
fred rosenberger wrote:
James O'Reilly wrote:when I type" java MyFirstApp" and I get an error.

it helps people help you if you tell us EXACTLY WHAT THE ERROR IS. Otherwise, folks are just guessing. are you getting a 'command not found' error? then it's a PATH issue or you don't have the JRE installed.

is it a 'class not found' erorr? then it could be a classpath issue.

Is it an exception at runtime? then it's something else. My point is that if you don't tell us exactly what the problem is, we're all just guessing, which is pretty much a waste of time.


I will let you know the "Exact Error" next time I am back on that computer (I am on another computer right now). I don't wish to inconvienience you your or waste your time. Sorry for the trouble.

Jim O
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Go back to the CLASSPATH you set and examine it carefully. Lots of sources give advice about how to set a system CLASSPATH, but that is unfortunately often out of date or mistaken.

If there was a system CLASSPATH before you set it, then it simply needs a dot which means "current location of the command prompt" better called "current directory" as an additional entry. If on Windows, stick .; at the beginning of the system CLASSPATH. If you end up with .; and ;.; and ;. in several places, that doesn't seem to do any harm.
If there wasn't a system CLASSPATH before you set it, and there is nothing you haven't added yourself, then delete the system CLASSPATH altogether. When you run an Java application, it will assume your CLASSPATH is "current directory" only.
If you do need a CLASSPATH, it is better to set it when you run your application, but it will probably be a long time before you are writing applications big enough to require a CLASSPATH.

I presume you have set a system PATH; unlike with CLASSPATH, you do have to set a PATH. You probably have got a PATH correct if "javac" works. Look at this Java™ Tutorials section about "common problems"; it tells you about what happens if your PATH isn't set.

Confusing feature: when you use "javac" you need to write the extension and when you use "java" you don't write the extension. So it will look something like this:
C:\MyJavaWork > javac MyClass.java
C:\MyJavaWork > Java MyClass
Whatever the output from MyClass is ;)
C:\MyJavaWork >
Suggest when you open your command line and wriite something like this
mkdir MyJavaWork
cd MyJavaWork
That way you can easily get to your Java work directory, and calling the second line will get your there next time you need it.

Don't add your Java installation directory to your CLASSPATH; that was necessary for some older versions of Java but is no longer required. Don't put your own work in the Java installation directory.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
James O'Reilly wrote:I don't wish to inconvienience you your or waste your time. Sorry for the trouble.
You aren't inconveniencing us or wasting our time, and there is nothing to say sorry about.
James O'Reilly
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Campbell Ritchie wrote:
James O'Reilly wrote:I don't wish to inconvienience you your or waste your time. Sorry for the trouble.
You aren't inconveniencing us or wasting our time, and there is nothing to say sorry about.


Hey Guys:

I pasted the exact error message from the windows command prompt so we are all on the same sheet of music. I will examine the documentation that you recommended as soon as I get a chance. Going to be kind of busy for the next 2 - 3 days, so I may not get to it till Wednes or Thursday. Thanks for all of the great help!!!

Jim

When I type: C:\Sun\SDK>java MyFirstApp



I get the following error:

Exception in thread "main" java.lang.UnsupportedClassVersionError: MyFirstApp (U
nsupported 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)

C:\Sun\SDK>
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
This kind of error is caused when you compile a source file with javac command that is newer version
than java launcher (java command).
Probably beside JDK there is also an older version of JRE installed on this computer,
and probably the PATH variable contains both references to the JRE/bin and JDK/bin directories,
but JRE/bin is earlier on the PATH than JDK/bin. So if you type javac command, you compile source with javac from JDK/bin
(javac command is only available in JDK/bin directory), but if you type java, the command from jre/bin is invoked.
Compare java & javac versions with commands:
javac --version
java --version
If this is the case, you can try to repair your environment manually, but I think the best and easiest remedy to this
is to completely uninstall/delete all versions of jre/jdk from your computer,
then download and install the latest version of JDK. Before renstalling check also:
- the PATH variable - remove all references to JRE/JDK,
- delete JAVA_HOME and JDK_HOME variables from the XP environment (if exist)
- it is also a good idea to remove java entries from the registry (run regedit, locate HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft and delete it),
and delete files from java cache (run start/run, enter %APPDATA% hit Enter, locate 'Sun' subdirectory and delete 'Java' subdirectory).
After this your machine should be completely cleaned from Java and you can install JDK again.

James O'Reilly
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Ireneusz Kordal wrote:This kind of error is caused when you compile a source file with javac command that is newer version
than java launcher (java command).
Probably beside JDK there is also an older version of JRE installed on this computer,
and probably the PATH variable contains both references to the JRE/bin and JDK/bin directories,
but JRE/bin is earlier on the PATH than JDK/bin. So if you type javac command, you compile source with javac from JDK/bin
(javac command is only available in JDK/bin directory), but if you type java, the command from jre/bin is invoked.
Compare java & javac versions with commands:
javac --version
java --version
If this is the case, you can try to repair your environment manually, but I think the best and easiest remedy to this
is to completely uninstall/delete all versions of jre/jdk from your computer,
then download and install the latest version of JDK. Before renstalling check also:
- the PATH variable - remove all references to JRE/JDK,
- delete JAVA_HOME and JDK_HOME variables from the XP environment (if exist)
- it is also a good idea to remove java entries from the registry (run regedit, locate HKEY_LOCAL_MACHINE/SOFTWARE/JavaSoft and delete it),
and delete files from java cache (run start/run, enter %APPDATA% hit Enter, locate 'Sun' subdirectory and delete 'Java' subdirectory).
After this your machine should be completely cleaned from Java and you can install JDK again.




Thanks Ireneusz:

That seems to make logical sense. I did install java version 5.0 instead of the 6.0, only because the textbook is based on release 5.0 (I bought the book last year). I will 1st try to eliminate all references to the JRE/JDK directory in my PATHs as you say and then try to execute my class. This would also explain why when I type PATH=C:\Sun\SDK\jdk\bin from the directory with my MyFirstApp class in it - the program runs properly from then on (untill I closed the windows black prompt screen, then I have to repeat the process, if I wish to re-run the class). I know now that I made a mistake not downloading 6.0, but wish to aviod a total re-install unless absolutely necessary, untill my java knowledge becomes more advanced. Removing the Path entry will probably make to difference that I need. I will keep you posted when I get back to it in a couple of days or so.

Jim O
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

You may confirm the JDK version used by your system by "java -version" in the command prompt...
James O'Reilly
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Vijitha Kumara wrote:You may confirm the JDK version used by your system by "java -version" in the command prompt...


Thanks Ireneusz:

I did a search as well as a version check 1st as you suggesed. The javac file is release 1.5.0_9 and the java.exe file is 1.4.2_03. When I perform the PATH command, they both change to 1.5.0_9. There are quite a few java.exe files on my PC since I purchased it in the end of 2002. There is even a copy in C:\windows\System32. I will attempt to de-install the Java 2 Runtime Environment,SE v1.4.2_03 in "Add or Remove Programs. And get version JRE 5.0 when I get a chance. I am a litle afread to manuallly perform "surgery" all over the different directories on my PC untill I get more deeply involved with my book. I am just happy that we now definately know what the problem is and I can live with typing PATH=C:\Sun\SDK\jdk\bin at the beginning of every windows command prompt session for the time being, if the JRE download of 5.0 doesn't fix the problem.

hank you everyone for helping me out and I will see speak to you soon.

Jim O
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
James O'Reilly wrote:I can live with typing PATH=C:\Sun\SDK\jdk\bin at the beginning of every windows command prompt session for the time being, if the JRE download of 5.0 doesn't fix the problem.
How long have you been using Windows? You definitely don't need to set the PATH at the beginning of each command window you launch. This isn't a Java issue, it is a Windows issue. However, this isn't the forum for basic Windows stuff. This is why I recommended a Windows for Beginners book earlier in this thread. It will teach you about topics like PATH, user environments, etc. - Without a firm understanding of such topics, you're going to have a real hard time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
The reason you are using a Java1.4.... file is that it is in the directory quoted first in the system PATH. I have already quoted the "common problems" link, which links to instructions about how to set your PATH.
James O'Reilly
Greenhorn

Joined: Jan 23, 2010
Posts: 9
Campbell Ritchie wrote:The reason you are using a Java1.4.... file is that it is in the directory quoted first in the system PATH. I have already quoted the "common problems" link, which links to instructions about how to set your PATH.


Hi Ireneusz:

Just wanted to let you know that I de-installed the JRE runtime environment 1.4.2_03 under add/remove programs and downloaded the Java runtime environment 1.5.22 version and every thing runs fine. I tried to modify the PATH parameter in windows across the time span on several days to point to C;\Sun|SDK\jdk\bin at the beginning and eliminate references to version 1.4 in a multitude of fashions and still had problems. I searched my hard drive and found about 5 java.exe as well as other java executables in different directories all over my PC, including in Windows/System32 and was apraid to start randomly deleting these files and getting too radical with altering windows system files as you can sometimes get hurt doing this. I much preferred to do it the "elegant way" thru add/delete new programs in control panel. Thanks everyone for your time in this matter and Take care.

Jim O

P.S. as a side note - I have been using windows since about release 3.0 in 1990...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Don't delete the Java installation from the Windows folder. Don't delete any of those folder, but use the control panel -> add and remove programs applet to get rid of them.

To use the Java installation you installed yourself, simply put its location starting "C:" (or similar) and ending in "bin" first in the system PATH.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

To set the path permanently,

Right click My Computer > Properties

System Properties dialog opens
Advanced Tab > Environment Variables

Environment Variables dialog opens
user variables section: New

I guess you know what to do from here on ...

[edit: removed as per Campbell Ritchie's mentioned point]


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You probably don't want a new user PATH, but to correct the existing system PATH.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

post corrected
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot run program from another directory other than c:\Sun\SDK\jdk\bin