• Post Reply Bookmark Topic Watch Topic
  • New Topic

CLASSPATH troubles  RSS feed

 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i ve a problem with the CLASSPATH. Ther's ths code:



If i compile, the error is HelloDate.java:2: error: package net.mindview.util does not exist

So i try to modify the Environment Variables. Under System Variables i add CLASSPATH with the value C:\Users\Ivan\Program\TIJ4-code (in this directory ther's all the code of the book, and ther're folders \net\mindview\util and there i find the Print.java file) and strangely (because before the code was compiled, but did not run because he could not find a class, can not remember which) the problem persists.

How can i solve? Greetings!
 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember you have to restart the command prompt after you save environment variables.

I don't like to add a classpath to my environment variables though. I prefer to have a src directory in my project, and then run the java command with the -cp switch:

javac -sourcepath src -d bin src/com/example/Main.java
java -cp bin com.example.Main
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Remember you have to restart the command prompt after you save environment variables.

I don't like to add a classpath to my environment variables though. I prefer to have a src directory in my project, and then run the java command with the -cp switch:

javac -sourcepath src -d bin src/com/example/Main.java
java -cp bin com.example.Main


Can you explain more clearly? I'm at the beginning and i don't know what those commands works (i've read what they do) but it's difficult to understand how solve the problem

The right directory of the CLASSPAT is C:\Users\Ivan\Desktop\Program\TIJ4-code.

Now javac works, but java, with all .java files give me the error: Error: impossible find or load principal class *****.

Following this that's my same question, i've solved, but i'm curious to know your proceedings.
 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us the command that you invoke to get the message, and the working directory of your command prompt?
 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To explain my own example, when I run javac -sourcepath src -d bin src/com/example/Main.java (with the working directory being my project folder), I'm telling the compiler to compile src/com/example/Main.java, (which contains com.example.Main). If the compiler needs sources of classes referenced in Main.java, -sourcepath tells the compiler where it can find those sources. In this case, the src folder. The -d switch tells the compiler to put all the .class files in the bin folder, so I don't mix up my sources and compiled classes. So after compilation, com.example.Main will be in bin/com/example/Main.class.

To run the program, I tell java to execute com.example.Main. All it needs now is to know where the class file is, which I specify with the -cp switch. I tell it to find classes in the directory where I compiled them to: bin.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:To explain my own example, when I run javac -sourcepath src -d bin src/com/example/Main.java (with the working directory being my project folder), I'm telling the compiler to compile src/com/example/Main.java, (which contains com.example.Main). If the compiler needs sources of classes referenced in Main.java, -sourcepath tells the compiler where it can find those sources. In this case, the src folder. The -d switch tells the compiler to put all the .class files in the bin folder, so I don't mix up my sources and compiled classes. So after compilation, com.example.Main will be in bin/com/example/Main.class.

To run the program, I tell java to execute com.example.Main. All it needs now is to know where the class file is, which I specify with the -cp switch. I tell it to find classes in the directory where I compiled them to: bin.


It works perfectly! Thanks!
But why you prefer this method?

 
Stephan van Hulst
Saloon Keeper
Posts: 7974
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, then all my code is in well organized places, and I don't have to mix up all sorts of project files with each other. I can put my program sources in a folder called src/main, test code in src/test, external libraries in lib, documentation in doc, output files in bin or target, settings in etc or conf. This makes it easy when you're looking for specific files.

It also means that I can actually have my projects completely separate from each other. Classes from different projects don't get mixed up in one big pile that you have on the classpath. You set the classpath specifically for the project you're currently working with using the -cp switch. This way, you can also have different classes that have the exact same name, for different projects.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Well, then all my code is in well organized places, and I don't have to mix up all sorts of project files with each other. I can put my program sources in a folder called src/main, test code in src/test, external libraries in lib, documentation in doc, output files in bin or target, settings in etc or conf. This makes it easy when you're looking for specific files.

It also means that I can actually have my projects completely separate from each other. Classes from different projects don't get mixed up in one big pile that you have on the classpath. You set the classpath specifically for the project you're currently working with using the -cp switch. This way, you can also have different classes that have the exact same name, for different projects.

Thanks! I find this method is very convenient!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!