Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I change the path environment?

 
Andy G
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - I'm a newbie using the Head First Java book. I've been trying to follow the instructions on the book on how to change the path, but they are not very helpful. Someone else posted the same question and I tried that, but it didn't work either.

I am using XP home edition. I downloaded the J2SDK from Sun�s website. I created a new folder on my desktop called java and saved the following code with .java extension:

public class MyFirstApp{

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

}

How do change the path and get it to compile? Please help!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can change it permanently in Start / Settings / Control Panel / System / Advanced / Environment Variables.

I have a bunch of different JREs installed and don't dare mess with permanent stuff because it might mess up my work life, so I prefer to set the path right before starting the program. I have batch files like:

Once that gets going, your next question is likely to be about classpath. The answer is the same right before the Java command:
 
Andy G
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went to Start / Settings / Control Panel / System / Advanced / Environment Variables and added "C:\Program Files\Java\jdk1.5.0_04\bin" Is there anything else I should do?
 
samdeep aarzoo
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
set classpath some.jar;%classpath%

instead of this if i use than

set classpath some.jar;.
is it correct
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding the bin dir to the path will make it so you can open a command window and type "java" or "javac" and those programs will run. That's a critical step for sure.

When you run your program it might say it cannot find classes that you imported from various places. That means the compiler knew the classpath to those classes and the runtime did not. If and when this happens, you'll need to add the path to those classes to your classpath.

I showed "set classpath = some.jar;%classpath%" because the classpath might already have some good things in it. This puts my new things in the front but preserves anything already there. If I have to add several things to the classpath I just have more commands in that format.

You could set up classpath permanently as well, but it's really something that changes depending on the program you're running, so it makes more sense to set it up every time you run.
 
Phil Dixon
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy,

I'm working my way through HFJ aswell. I added two lines to autoexec.bat

autoexec.bat

This worked for me although this is only my second week as a would be Java programmer.
 
Andy G
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help guys!

But know everytime I try to run the class i get the following error:

Exception in tread "main" java.lang.NoClassDefFoundError

I am trying to run:

public class MyFirstApp{

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

}
 
Andy G
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the error i am getting:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic