• Post Reply Bookmark Topic Watch Topic
  • New Topic

Environment Variables  RSS feed

 
Louis Kounios
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys! My name is Louis and I'm a newbie in Java and Javaranch.

I've been having problems with running (not compiling) my class files.
Here's my previous working setup:
I store my .java files in C:\Java Programs. Now, I'm almost at the end of the Head First Java book and I'm at the chapter about releasing our code. What I do is I use the command prompt to browse to C:\Java Programs, and then execute "javac <Filename>.java". Compilation works like a charm. Then, I simply moved the .class file to the \bin\ directory and executed "java <ClassName>" (without the .class). This was my previous setup, but now that I've read the chapter, I want to keep my files organised in the form "<ProjectName>->"classes" & "source" folders -> "com" folder -> "louiskounios" (my unique name) -> .java files (in the source folder). What I do now is go to the the project's source directory and use the "javac -d ../classes com/louiskounios/*.java" command to compile my files. Compiling is successful and my class files are created in "../classes/com/louiskounios/".

The problem is running those classes! I change directory and go to the classes directory of my project, and then run "java com.louiskounios.<ClassName>, but I get the annoying classNotFoundException or whatever it's called. I've literally tried everything, read 100 different suggestions online and nothing seems to be working. I want to really understand how the CLASSPATH variable works etc. My environment variables are set up as follows:
JAVA_HOME = C:\Program Files\Java\jdk1.6.0_22
Path = %JAVA_HOME%\bin\
CLASSPATH = %JAVA_HOME%/jre/lib;./;

From what I can gather, CLASSPATH is used to tell your "java" command where to look for .class files. I think "./" tells the java command to look in the current working directory in command prompt.

Can you guys please help me? Sorry for the long post, but I'm interested in understanding how the environment variables work, and not just fix the problem so I can run my .class files.
 
Ed Ward
Ranch Hand
Posts: 147
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings Louis!

1. You do not need to have %JAVA_HOME%/jre/lib on your class path. Anything there is automatically on the classpath.

2. You do NOT need to be in your project directory to execute your class files which have been declared to be in a package.

3. You DO need to have the root directory that contains the class files you want to execute on your class path, assuming those classes are in a package. So, if the class file you want to execute is in a package named "com.louiskounios" and that is in a directory named "classes" then set your class path to C:/whateverthepathis/classes or include it on the command line like this:

java -cp "C:/Java Programs/classes" com.louiskounios.<ClassName>

Note that to execute the above my current directory could be C:/someotherdir

If the class you want to execute is not in a package, then you should not need to specify a classpath, if you are in the same directory that the class file is in. Assuming my current directory is "C:/Java Programs/classes", the command would look lik this:

java <ClassName>

It sounds like you were previously were not putting them in a package which is why you had no problems. Now that you are putting them in a package you need to tell the JRE where to start looking for the package name.

Extra: If the class files you want to execute are in a jar file, then the full path to the jar itself needs to be included on the classpath and then the fully qualified class name. In that scenario, the above command might look like this:

java -cp "C:/whateverthepathis/jarfile.jar" com.louiskounios.<ClassName>


Hope this gets you have the hurdle!
 
Louis Kounios
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! I understand how things work now and everything works like a charm. Thank you!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice post, Ed!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!