Re: set classpath correctly
Mitchell_Collen wrote:
Knute Johnson wrote:
Lew,
Does the commands that you provided have to typed every everytime I open a
[quoted text clipped - 19 lines]
java -classpath ../build/classes com.lewscanon.foo.Main
java -cp ../build/classes com.lewscanon.foo.Main
99.9% of the time you don't need a CLASSPATH. You aren't going to have
to type it ever probably or if you do it will be in your build script.
When you make your package directories, use a single root and life will
be simple.
Yes but isn't that only if I use an IDE. I don't have an IDE.
You are not listening. You don't need a CLASSPATH. I write Java code
for a living and I can't remember the last time I needed a CLASSPATH for
production code. I don't use an IDE, I use VIM. Even if your code is
in packages you don't need a CLASSPATH. You do need to set the PATH so
you can execute the compiler but that's it. Just about everything else
will work without a CLASSPATH. About the only time I use a CLASSPATH is
if I compile a program in a package and I want to run it from the
directory that it lives in. Then I type java -cp \ mypackage.MyClass.
That's it.
Set up your path so that you can execute the compiler from the command
line, set up your package/directory structure so that everything has the
same root directory (I use \). All of my programs are in
/com/knutejohnson/somepackages. I write a simple batch program to
compile and jar the classes. Here is an example;
pushd \
javac com/knutejohnson/package1/MyClass.java
if %errorlevel% neq 0 goto done
jar cvfe com/knutejohnson/package1/*.class
com.knutejohnson.package1.MyClass
:done
popd
del *.class
If I'm not going to jar them up, say I'm just testing, from the
directory of the .java files;
javac MyClass.java
java -cp \ com.knutejohnson.package1.MyClass
to execute the loose class files.
I know that CLASSPATH is one of the most confusing aspects of Java
programming. I had a terrible time figuring it out myself. For more
advanced details, see my web site;
www.knutejohnson.com/classpath.html
--
Knute Johnson
email s/nospam/knute/