Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes How works the classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How works the classpath" Watch "How works the classpath" New topic
Author

How works the classpath

Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
Dear friends,
I have an question about the classpath.

Since I work with te command prompt (I compile programmes in MS Dos) I have problems.

For example: I compile programs on D: Java\Bin\<classname.java> .Then I have no problem.

But I want to make a projectmap in the direcory Bin for example "food" where I can put my sourcefiles. I also want to compile the classes in in that folder.
The directory will be: D: Java\Bin\food\<classname.java>

Then I go problems.

How can I set the classpath so that it works?.
Where can I find the classpath?

I can't find it in the book Head first Java.
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Check out:
http://faq.javaranch.com/java/HowToSetTheClasspath

it a great explanation there!
Let me know if you still find problems.

Regards,
John Eipe


www.cs-repository.info
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
You ought not to compile anything in the "bin" directory. You ought to create your own directory to put your work in.
It is best not to set a system classpath at all, in which case the JVM will take "." (ie current directory) as a default for the classpath. What it means is, if you download some Java files (most commonly in .jar format) which you need for your application, you add the address of those files to your classpath, then the compiler and "java" tool can find those files.

If you want to compile in a particular directory, try this sort of thing, immediately after opening your command prompt window
mkdir java
cd java
mkdir food
cd food
javac -d . BeefSteak,java
That will create the directories, then you need to save the .java files in the food directory after writing "mkdir food" and before writing "javac . . ." Note that using the -d option will create subdirectories corresponding to the package names in your .java files. If the BeefSteak class has a dependency in another file, that file must be compiled first. Or tryor similar, orI don't know whether you have to name the classes in any particular order when compiling.
You don't need to do anything with the classpath for that sort of compiling.
Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
I put my sourcefiles Apple.java and Fruit.java in the food map directory D:\Java\Bin\Java\food

Then I tried to compile with a command : javac Fruit.java. I got an error but when I compile in the directory D:\Java\Bin the source file Fruit.java with the command javac Fruit.java then it compiles and it makes a class file of Fruit.

Can you explain this?


I don't know how I can compile in anoher directory then the bin-directory.
I also have no classpath. It is no problem I read.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Maybe you have two files called Fruit.java

When you get to the bin directory (which is by the way incorrectly named; you should restrict "bin" to directories containing binary files) try cd java\food. It may be that there is an error in your Fruit file. You really ought to say what the error was; "I got an error" doesn't enable us to help at all.

If you have no system classpath set, you ought to leave well alone. Your Java™ installation will work nicely without it. You usually cause more problems than you solve by setting a system classpath.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I'd like to hear some feedback about this closed post too.


[My Blog]
All roads lead to JavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
He says a Fruit.class file has be created, so it would appear the PATH has been set correctly.

I think part of the problem is that we haven't been given enough detail about what the errors are. And there are lots of spelling errors in the details (eg PATHCLASS).
Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
Campbell Ritchie wrote:He says a Fruit.class file has be created, so it would appear the PATH has been set correctly.

I think part of the problem is that we haven't been given enough detail about what the errors are. And there are lots of spelling errors in the details (eg PATHCLASS).


I got the erorr: "unkown source". I think it has nothing to do with the fact that there is someting wrong with my code but that the compiler cannot found the class I want to compile.

Yesterday I downloaded a programm Blue J (A java programm) and now I have no problems anymore. When I run the Fruit class in another directory then the bin-map it works.

By the way I still have no PATHCLASS...... I appreciate it that you guys are thinking with me. I still don't know what the solution is but I can study further. Thank you for trying to help me.






 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How works the classpath