• Post Reply Bookmark Topic Watch Topic
  • New Topic

javac path issue  RSS feed

 
Joe Wibbs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I recently installed Java JDK 7 on my PC running Windows 7 and I'm looking to solve an issue with an error message I'm receiving.

Installation seemed to go just fine. javac.exe and java.exe are installed in C:\Program Files\Java\jdk1.7.0_40\bin

I added that exact address to the beginning of the System Variable "Path" in Environment Variables and I added a semi-colon to the end:
C:\Program Files\Java\jdk1.7.0_40\bin;


I opened a command prompt and java -version returns:
java version "1.7.0_40" etc. etc.

javac -version returns:
javac 1.7.0_40

path returns:
PATH=C:\Program Files\Java\jdk1.7.0_40\bin; etc. etc.

I have a file "Example.java" located in C:\Program Files\Java\jdk1.7.0_40\bin

So when I try to compile the file at the C:\> by typing "javac Example.java" I receive the following error message back:
javac: file not found: Example.java

If I navigate to the directory though by typing cd\Program Files\Java\jdk1.7.0_40\bin and then typing "javac Example.java" the Example.class file is created as expected.
Please advise!


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Wibbs wrote:Hi, I recently installed Java JDK 7 on my PC running Windows 7 and I'm looking to solve an issue with an error message I'm receiving.

Installation seemed to go just fine. javac.exe and java.exe are installed in C:\Program Files\Java\jdk1.7.0_40\bin

I added that exact address to the beginning of the System Variable "Path" in Environment Variables and I added a semi-colon to the end:
C:\Program Files\Java\jdk1.7.0_40\bin;


I opened a command prompt and java -version returns:
java version "1.7.0_40" etc. etc.

javac -version returns:
javac 1.7.0_40

path returns:
PATH=C:\Program Files\Java\jdk1.7.0_40\bin; etc. etc.

I have a file "Example.java" located in C:\Program Files\Java\jdk1.7.0_40\bin

So when I try to compile the file at the C:\> by typing "javac Example.java" I receive the following error message back:
javac: file not found: Example.java

If I navigate to the directory though by typing cd\Program Files\Java\jdk1.7.0_40\bin and then typing "javac Example.java" the Example.class file is created as expected.
Please advise!


It is not a PATH issue -- the javac program is running fine. The error message is from the javac program. It is complaining that it can't find the Example.java.

Henry
 
Joe Wibbs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that is strange because it complains only when I try to run javac Example.java from the C:\>.
When I navigate to the \bin directory using cd\ and type javac Example.java the Example.class file is created as expected.
Then running "java Example" returns the expected output.

Using Windows Explorer, I also see the Example.java file in the bin directory and it shows "JAVA File" for the file attribute "Type". I created the Example.java file in Microsoft's WordPad program.

Any other thoughts?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Wibbs wrote:Ok that is strange because it complains only when I try to run javac Example.java from the C:\>.
When I navigate to the \bin directory using cd\ and type javac Example.java the Example.class file is created as expected.
Then running "java Example" returns the expected output.

Using Windows Explorer, I also see the Example.java file in the bin directory and it shows "JAVA File" for the file attribute "Type". I created the Example.java file in Microsoft's WordPad program.


javac doesn't use the PATH variable to find the java file. It uses a source directory, if provided -- which you didn't. Otherwise, it takes it from the command line... so when you ran "javac Example.java", it is looking for the java file in the current directory. So, when you are in the same directory as the java file it works.

So... nothing strange. It is working as expected.


BTW, why is the java file in the bin directory? It is probably better to create a project related directory for the source files.

Henry
 
Joe Wibbs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I will create a project related directory for the source files.

But I still am searching for a solution that allows me to run javac from the C:\ prompt so I don't have to navigate to the bin directory (or the project directory) each time.
There are hundreds of posts that suggest putting the bin directory at the beginning of the path variable is the solution--but no luck for me.
So I'm definitely confused at this point.
 
Joe Wibbs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p.s. Here are the relevant Path instructions from Oracle's java installation website:
http://www.oracle.com/technetwork/java/javase/documentation/install-windows-142126.html

4. Update the PATH variable (Optional)
You can run the JDK without setting the PATH variable, or you can optionally set it as a convenience.

Should I set the PATH variable?
Set the PATH variable if you want to be able to conveniently run the JDK executables (javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command. If you don't set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

C:> "\Program Files\Java\jdk1.6.0_<version>\bin\javac"
MyClass.java
It's useful to set the PATH permanently so it will persist after rebooting.

How do I set the PATH permanently?
To set the PATH permanently, add the full path of the jdk1.6.0_<version>\bin directory to the PATH variable. Typically this full path looks something like C:\Program Files\Java\jdk1.6.0_<version>\bin. Set the PATH as follows on Microsoft Windows:

Click Start > Control Panel > System on Windows XP or Start > Settings > Control Panel > System on Windows 2000.
Click Advanced > Environment Variables.
Add the location of bin folder of JDK installation for PATH in User Variables and System Variables. A typical value for PATH is:
C:\Program
Files\Java\jdk1.6.0_<version>\bin
PATH environment variable is a series of directories separated by semi-colons (;) and is not case sensitive. Microsoft Windows looks for programs in the PATH directories in order, from left to right.
you should only have one bin directory for a JDK in the path at a time. Those following the first instance are ignored. If one is already present, update it to jdk1.6.0_<version>\bin.
If you are not sure where to add the path, add it to the right end of the PATH in the User Variables.
The new path takes effect in each new command window you open after setting the PATH variable.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Wibbs wrote:But I still am searching for a solution that allows me to run javac from the C:\ prompt so I don't have to navigate to the bin directory (or the project directory) each time.
There are hundreds of posts that suggest putting the bin directory at the beginning of the path variable is the solution--but no luck for me.
So I'm definitely confused at this point.

You have one. You can run the java program from anywhere, as you have demonstrated.

BUT...

The javac program needs a file name (or several file names) as an argument. It has to then be able to find the file. You have two options:

1) navigate to the directory where the .java file is.

You say you don't want to do that. Ok, then your other option is:

2) Pass javac.exe the full path to the file...something like:

c:\javac.exe C:\org\bjc\fbr\project1\java\MyJavaFile.java
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a file "Example.java" located in C:\Program Files\Java\jdk1.7.0_40\bin


A sidenote:
It is a very bad place to put sources and to compile them there!
You should leave that directory (and the other deployment ones) alone.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!