aspose file tools*
The moose likes Beginning Java and the fly likes path variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "path variable" Watch "path variable" New topic
Author

path variable

nirali shah
Greenhorn

Joined: Apr 16, 2009
Posts: 24
C:\Program Files\Java\jre1.5.0_06\bin

this is the path for my java bin files.

Now when I try to compile my java class at command prompt as "javac SomeClass.java" , I get an error message "'javac' is not recognized as an internal or external command,
operable program or batch file."

Does it mean that path I have set is for compiled program ? i e if I want to run java SomeClass, then this will work ? So which path do I need to set to compile the program ?
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2891
    
  19

Howdy Nirali,

The problem is that command "javac" is not indentified. Do a search for the file "javac.exe" and discover the directory (folder) where that file is placed. And then, you have to set the path to that directory.

Devaka.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Shahnawaz Shakil
Ranch Hand

Joined: Aug 04, 2008
Posts: 57
You have two options:
1) In the command prompt write set path=C:\Program Files\Java\jre1.5.0_06\bin and then you can compile the program by simply typing javac anyProgram.java
2)Write click my computer, go to environment variable and add the the path C:\Program Files\Java\jre1.5.0_06\bin to the Path variable

If you chose the 2nd option, you will need not to set the path every time you open the command prompt.
nirali shah
Greenhorn

Joined: Apr 16, 2009
Posts: 24
I did following.
I kept my source code directly under C:\
At the cmd prompt, I executed following exe file "C:\Program Files\Java\jdk1.6.0_11\bin>javac c:\AnyClass.java" So the source code did compile and class file is created under c directory.
Now when I do "C:\Program Files\Java\jdk1.6.0_11\bin>java c:\AnyClass"., I get following error

------------------------------------------------------------------------------------

C:\Program Files\Java\jdk1.6.0_11\bin>java c:\AnyClass
Exception in thread "main" java.lang.NoClassDefFoundError: c:\AnyClass
Caused by: java.lang.ClassNotFoundException: c:\AnyClass
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: c:\AnyClass. Program will exit.
------------------------------------------------------------------------------------------

I tried to keep classfile in different folder and try to execute from there, but same error.

Whats this due to ? How to correct this ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
While you are at it, why not upgarde to Java6?

It is actually set path=C:\Program Files\Java\jre1.5.0_06\bin;%PATH% or something like that. Otherwise other programs in the PATH will vanish. And if you set up the PATH like that, you will only have that PATH for the lifetime of that command prompt window. Fuller instructions can be found by looking up your problem in this "common problems" page.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874




C:\Program Files\Java\jdk1.6.0_11\bin>java c:\AnyClass
Exception in thread "main" java.lang.NoClassDefFoundError: c:\AnyClass


You cannot run java command like that , java excepts the file name and " not the absolute pathname of the file " , the file can be in the CLASSPATH directory and

Add C:/anydirectory to your CLASSPATH , place your class file inside C:/anydirectory ( if its packagee , place entire package) and run "java anyClass" from anywhere ( any path i mean : ))
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

nirali shah wrote:C:\Program Files\Java\jre1.5.0_06\bin




you are trying to execute javac which needs jdk
Prabz Bhatia
Greenhorn

Joined: Apr 14, 2009
Posts: 19
This is all getting confusing....
First things first,
1. You need to have jdk installed to compile java files . Default Install directory is : "C:\Program Files\Java\jdkXXXXXX" where XXXXXX is the installed jdk version.
2. Set value of the PATH variable as follows:
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
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.

3. To compile any java file, cd to the directory where it is located and type the command:
Directory>javac <yourFileName>.java

4. To execute a class file, cd to the containing directory and type:
java <yourClassName>


Hope this helps.....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
That should help. I would add:

It is a good idea to create a "java" folder to keep all your .java and .class files.
Nishant Arora
Greenhorn

Joined: Apr 21, 2009
Posts: 16
You must download java jdk1.6.0 or something like that. Set environment variable modify path to add jdk1.6.0 bin path.
in command prompt type this : path c:\program files\java\jdk1.6.0\bin or you can create batch file also.
Frankey James
Ranch Hand

Joined: Sep 27, 2008
Posts: 50
The problem is that he has his path set to a JRE bin folder, not a JDK. JRE install does not include javac utitily, only java and some others. You need JDK to get the javac utility.


Simple fix: download the JDK, NOT the JRE. Then go find the bin folder for that as you did with the JRE, append it to the PATH variable, then you'll be fine. Delete yor JRE reference as well from the path variable. Nothing will happen if you don't. Windows will simply look in that path first for javac/java commands but won't find the javac command and thenw ill go to the next entry in the PATH variable (the right one with the JDK path).

You can simply type javac at the ommand prompt to test it. Remember, if you leave a command prompt/shell open and do these changes, they won't take affect until you cloe and open a new shell.


ho, ho, ho
jack wenttohill
Greenhorn

Joined: Apr 26, 2009
Posts: 9
Lots people get confused with this.

Windows OS Shipped with default JRE not JDK
If your system does not have JRE, Any site which has applet can prompt you to download JRE automatically to your system. And you might have JRE in that scenario.

JRE: Runtime environment.
JDK: Development kit

So you can not develop java application without a JDK, what it names speaks a lot.

So in JRE you will not find java compiler which is javac.

Do not confuse yourself. Java is an open standard as per the rule every OS must have JRE.

So do not consider using JRE you can develop java program. You can run though, but to develop you must download JDK from sun site
http://java.sun.com/javase/downloads/index.jsp

And after download and install point your path till bin directory.

Simply for Windows
Prompt:> set path=%path%;JDK_INSTALLED DIR_HOME_PATH/bin;
e.g, set path=%path%;C:\Program Files\Java\jdk1.6.0_06\bin;

Simply for Linux
Prompt:> export PATH=$PATH:/JDK_INSTALLED DIR_HOME_PATH/bin
e.g, export PATH=$PATH:/usr/java/jdk1.6.0_06/bin

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: path variable
 
Similar Threads
Problem with Chapter 5 - SimpleDotCom
Not able to run java
How to run a java class in console
creating .class file in Tomcat
Class file not getting created