File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help with setting the classpath for command line compiling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with setting the classpath for command line compiling" Watch "Help with setting the classpath for command line compiling" New topic
Author

Help with setting the classpath for command line compiling

Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
I do not understand how to set the classpath to compile on the commandline. can someone please help!!!


Duct tape is like the Force. It has a dark side, a light side, and it binds the whole universe together.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Cody Long wrote:I do not understand how to set the classpath to compile on the commandline. can someone please help!!!


well, for compiling, the directory that contains javac.exe should be in the path, not the classpath. javac.exe should be in a directory called bin, which is a subdirectory of your java installation directory. Do you know how to set the path?

classpath should not be an issue for compiling. class files are you get after you have compiled.

classpath is relevant when you have .class files that are in a different directories than your current directory. As long as all the class files you have created for a particular program are in the current directory, you shouldn't have to worry about classpath. You do have to set classpath sometimes when you have special class files when you have customized your default java environment.

Do you know how to set environment variables for a windows computer? That is where path and classpath are set.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
this page may help you.

http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The classpath, however, *does* need to include all the libraries the classes being compiled reference. Hopefully it's a rare occasion you have .class files in your current directory when you're compiling--that would indicate that you're not using a good package hierarchy.

The classpath can either be set as an environment variable or as a javac parameter. How to set the environment variable depends on what OS you're using, for javac see the OS-appropriate docs.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
David Newton wrote:The classpath, however, *does* need to include all the libraries the classes being compiled reference. Hopefully it's a rare occasion you have .class files in your current directory when you're compiling--that would indicate that you're not using a good package hierarchy.

The classpath can either be set as an environment variable or as a javac parameter. How to set the environment variable depends on what OS you're using, for javac see the OS-appropriate docs.


Well, I certainly would not want to be wrong about something like that. I must be missing a point. somewhere. I've been programming for a few years, and I always keep source files for an application in the same directory, fpr convenience, cause there was no reason not to. The only time I've had to worry about classpath is recently when I added a .jar file for JDBC driver to my classpath.

I suppose if you are creating sophisticated applications as a java professional, that might be more of an issue. But for just getting started as a beginner? OK but I know you know your stuff, so what am I missing here?

ok, unless you are talking about not grouping all the files for an application you have written together, then yeah, I can see that is different.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Even the simplest of applications benefit from a modicum of structure and organization; packages are a trivial, built-in way of providing some of that.
Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
Thank you fred that page helped and i figured out how to use javac, etc. Thank you all for helping!








BTW i always put my code in packages
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
David Newton wrote:Even the simplest of applications benefit from a modicum of structure and organization; packages are a trivial, built-in way of providing some of that.


Well, I guess I've been a little lazy in that regard, I haven't really bothered with packages because I haven't really needed them. But I'm not a professional developer. That being said, I do have one question. It is my understanding that it is not so much packages itself that requires the use of ClassPath, rather it is when you have built up your own libraries of classes that you want to re-use, and you don't want to make a copy of them in the current directory. Do you agree with that?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If your programs don't require any organization beyond the default package and class files that's fine, but that's not related to whether or not you make money programming (or whatever your definition of "professional" is).

It is my understanding that it is not so much packages itself that requires the use of ClassPath, rather it is when you have built up your own libraries of classes that you want to re-use, and you don't want to make a copy of them in the current directory. Do you agree with that?

A classpath is required unless everything is in your current directory (what package the classes are in doesn't matter, as long as the .class files are where they should be package-wise), in an unarchived (not in a jar) format, and nothing else has set a non-default classpath.

I don't think anybody said there's a link between packages and needing a classpath, only that the classpath must include all the libraries the classes being compiled reference, regardless of whose they are. If the default classpath is enough, that's fine, but it's still the classpath.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
OK thanks for taking the time it's a lot clearer now. I had never given much thought to a default CLASSPATH, which is probably why I couldn't follow what you were saying initially.

In regards to my comment about not being a professional developer, I only meant that I can imagine I'd be seriously handicapped as a professional if I refused to use packages, or a CLASSPATH, or an IDE. As an amateur hobbyist I can get away with it.

regards, learned some useful stuff here.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In regards to my comment about not being a professional developer, I only meant that I can imagine I'd be seriously handicapped as a professional if I refused to use packages, or a CLASSPATH, or an IDE. As an amateur hobbyist I can get away with it.

It's really just a matter of choosing what to spend time with--for me, Java is irritating enough that I'd rather have my tools do as much of the busywork as possible so I can focus on other tasks.

I'd also rather not re-write things that other people have written and tested extensively when dropping a library into my project solves wads of problems (the Apache Commons libraries being a prime example).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with setting the classpath for command line compiling