This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Setting up Java on WinXP: PATH creation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting up Java on WinXP: PATH creation?" Watch "Setting up Java on WinXP: PATH creation?" New topic
Author

Setting up Java on WinXP: PATH creation?

John M Green
Greenhorn

Joined: Jun 13, 2006
Posts: 6
I'm trying to set up the JDK to run on my Windows XP machine; I added the line it suggested in the documentation to the settings box where it says Path, but when I try to run %javac ... from the command prompt, it doesn't recognize it.

Any suggestions?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

The JDK installation notes for Windows explain how to set the PATH on different versions of Windows. Follow these to the letter and it should work.

The compiler is called "javac", not "%javac".
[ June 13, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
John M Green
Greenhorn

Joined: Jun 13, 2006
Posts: 6
Thanks!

I'm just getting started in all this so it's kind of difficult. I wrote my code in WordPad, but I'm not sure what to do next. How do you activate the compiler? do you run it from the command prompt or some other way? Thanks.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

be careful using something like wordpad. you don't want ANY formatting - plain text only.

i'm not sure if wordpad does this, but one of the editors also likes to tack an extension on the filename. make sure the file is really called whatever.java, and not whatever.java.txt

to fire up the compiler, go to a command prompt. cd into the directory where your .java file is. they type

javac whatever.java

hopefully, this will work. assuming it does, you would then type

java whatever

to run your program.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John M Green
Greenhorn

Joined: Jun 13, 2006
Posts: 6
thanks for the tip about the .txt extension; I had already been warned about that and made sure all the settings were correct. I'm using the documentation from the java website to set up the Path.

great advice, thanks!
John M Green
Greenhorn

Joined: Jun 13, 2006
Posts: 6
Okay, I finally got my path sorted out, but now everything I try to run comes back with one error. The actual wording is:

-------

C:\DOCUME~1\JOHNMA~1\>javac MyFirstApp.java
error: cannot read: MyFirstApp.java
1 error

-------
and my code looks like:
------

public class MyFirstApp{

public static void main (String[] args) {
System.out.print("I rule!");
System.out.print("The World");
}
}

-------------------
I'm using the book Head First Java by Kathy Sierra & Bert Bates from O'Reilly...any suggestions? (besides using another book at the moment)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

forgive me asking the obvious, but you are sure you are in the correct directory? in other words, when you do a

dir MyFirstApp.java

does it actually show your file?

and two minor other things...

1) THANK YOU for posting the exact error message. you have no idea how often somebody says "i tried compling, and it didn't work. how do i fix that?"

2) it would help if when you post code, you surround it with the UBB "code" tags. after you paste your code in, higlight it and press that little button below the text box, and you should see [ code] and [ /code] (without the spaces) around your code... this will preserve the formatting.
[ June 13, 2006: Message edited by: fred rosenberger ]
John M Green
Greenhorn

Joined: Jun 13, 2006
Posts: 6
Hi fred,

Okay I went back to the Java website and walked through the tutorial (should have done that first, obviously) and now everything compiles and runs as it should.

My only trouble now is correctly setting the PATH variable in the System box. There's already a bunch of stuff in there and I'm afraid to delete it all for fear I may mess something else up; however I seem to have done it right but it's not working.

UPDATE:

Alrighty, now I seem to have the path set correctly since it shows up in the command prompt just like the JDK installation notes say when I type 'path' a the command prompt, but now how do I use the new path variable when I want to run the compiler or a java program? I don't know what to type to get that to work. Right now I'm having to change the directory in the command prompt whenever I open it up again to the directory where my files are living; then they compile and run just fine, but I thought the point of setting the path was so you don't have to change the directory and type it in every time?

I'm tempted to just use an IDE, but I read it's better to see it all in the command box when you're starting out.

suggestions?
(thanks in advance, you are all very helpful)

[ June 13, 2006: Message edited by: John M Green ]
[ June 13, 2006: Message edited by: John M Green ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Originally posted by John M Green:
Thanks!

I'm just getting started in all this so it's kind of difficult. I wrote my code in WordPad, but I'm not sure what to do next. How do you activate the compiler? do you run it from the command prompt or some other way? Thanks.


Follow Sun's "Hello World" tutorial step by step, it explains in baby steps what to do to write, compile and run your very first Java program and explains a number of common beginner mistakes and their solutions.

I'm sure you'll get it working if you follow the steps of that tutorial very carefully.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

the PATH variable lets you call javac and java from anywhere by simply typing "javac" or "java". you don't have to type "C:\Program Files\Java\jdk1.5.0_03\bin\javac" every time.

it is usually still easier to move to the dir where your java files are and compile them there. you don't HAVE to, but then you'd have to give java the full path to the files...

i.e. if you were in c:\, but the java files were in the c:\fred\project1\ directory, you would have to do this:

c:\>javac c:\fred\project1\MyClass.java

did that make sense?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
WordPad? I ought to have told you before, but there are far better editors. WordPad is a basic word processor, but programs aren't written in WP. They are written in plain text.
My favourite text editor for Java programming is called JCreator and you can download it here. As well as doing things like automatically matching { and }, and putting different colours on different parts of your program, it even has a function whereby you can compile and execute a file from the JCreator toolbar.

. . . but you have to tell JCreator where your PATH and CLASSPATH are first.

CR
John M Green
Greenhorn

Joined: Jun 13, 2006
Posts: 6
fred,

Yes, it compiles when I change directories with the >cd C:\<etc> command, but I thought it was supposed to compile from any directory using PATH. I have not yet set my Classpath variable, so maybe that's why it won't do it. When I type 'path' at the prompt, it gives the path correctly, so it's kind of confusing since I've triple checked it to make sure it's right. I'm using Windows XP Professional edition.

Campbell, thanks for the tip about the text editor. I have made sure that WordPad isn't adding a .txt file extension and it's running fine, but I like the idea of an editor that checks to match my {} and the colors thing.

Thanks for the help,
JMG
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

setting the path lets you run the java compiler from any directory, without typing the full path of where the compiler is. However, you still have to then tell the compiler where the .java files are. by default, it looks in the current directory. so, you can cd into the directory where the .java files are, type "javac MyClass.java", and it should work.

if you don't want to cd into the dir with the .java files, you'd have to do something like

javac c:\practice stuff\project1\java\MyClass.java

and then, your .class files would end up in the currect directory.

I guess i'm doing a poor job of answering your question...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Setting up Java on WinXP: PATH creation?