Skip to main content

how to set the path so that we can run java from anywhere

3 replies [Last post]
anitha2324
Offline
Joined: 2007-10-21

Hi All,
I have installed java 1.5 on my machine ( windows 2000 server ), the java installed on the following location ( c:\java\jdk1.5.0_03)

i am trying to run java programs from anywhere , i have added to the PATH variable the following entries ( in both user variable and system variable ):

C:\Java\jdk1.5.0_03\bin;C:\Java\jre1.5.0_03\bin;

but when am trying to run any java program from the following location am getting errors

c:\javac example1.java
error: cannot read: example1.java
1 error

so is my way is right or there is some other way to accomplish that , please guide me , am newbie to java

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
anitha2324
Offline
Joined: 2007-10-21

Hi thanks alot for your reply ,

i have installed java in c: drive , and my java programs are stored in the bin directory ( the full path is C:\Java\jdk1.5.0_03\bin; )

so example1.java is stored under bin directory (C:\Java\jdk1.5.0_03\bin)

to run my java programs i am doing the following

1.go to start --> run --> cmd
2. from the dos command i used to be in the following path

C:\Java\jdk1.5.0_03\bin>

and then i run my programs like

C:\Java\jdk1.5.0_03\bin>javac example1.java

and i then i run the my java program using

C:\Java\jdk1.5.0_03\bin>java example1

my question is now i want to run my programs without going to the full path (C:\Java\jdk1.5.0_03\bin>)

i want to run it from c:\> directly ,

i have read some articles about how to set the path and the classpath so that i can run java program from any location by referring the following links

(http://www.jibble.org/settingupjava.php

http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html)

but i did not successed in that

Please let me know the correct steps to set the path and the classpath so that i can run java program from any location

pietblok
Offline
Joined: 2003-07-17

Hi Anitha,

Well, it is not the PATH that is causing you trouble: java and javac are found. Correct?

When compiling (javac) you will have to tell the compiler where to find your java source files. Whatever you do, you still can't expect javac to find them at random. And also, you must tell the compiler where to write the output class files.

You should add some directory to your CLASSPATH where those dot.class files are placed (remember the package structure). If you do so, you can run your java programs from anywhere.

Just a question: you say you are using java 1.5.0.03. Why is that? If you MUST use java 1.5, there are more recent updates available. If you do not have a constraint for java 1.5, why not use java 1.6? Did you perhaps (just guessing) install java from some CD that came with a book? If so, what does the book say about setting up your environment?

Something else that strikes me. You seem to think that your example source (or executable class file) is in the JDK bin directory. That may be the case, but it sure is a strange place for them to reside in.

I have the impression that you are quite new to java. That is OK. Anyone must start somewhere :) and you are confused about how to compile and run a program. Maybe you should first try reading the documentation and do exactly what is said there. Then, in a later stage, try experimenting with classpath settings.

Anyway, don't let these starting problems deter you from using java.

Good luck,

Piet

pietblok
Offline
Joined: 2003-07-17

Hi Anitha,

Where is example1.java located? The way you invoke the java compiler indicates it is in the root of your C drive.

Lets assume you keep java sources in C:sources

Lets also assume your example1 class is in package xxx.yyy.

Then the example1.java file should be in directory C:\sources\xxx\yyy

To compile it, go to directory sources:

cd C:\sources

And compile as follows:

C:\sources>javac xxx/yyy/example1.java

As an alternative, execute javac -help. That gives you all possible options (as where your source files are located and where the compiler output should be written. (Or consult the documentation on javac).

Hope this helps

Piet