I need to write a simple program in Java and would love to do it the same way I program in Python and Ruby using TextMate. I can write and run a simple Hello World program, but cannot find a way how to load external libraries. I just need to compile with all *.jar files from the working directory (or do I need to intall them first?). This is my first Java-experience, so I would appreciate a detailed answer.
I do prefer to code in TextMate not an IDE, so I wonder if t开发者_如何学JAVAhis is possible with Java.
To be able to use classes stored inside a jar file in your program, this jar file must be part of the classpath.
Read http://en.wikipedia.org/wiki/Classpath for explanations on how to set the classpath. Note that this is also true when compiling your program.
Suppose you're at the root of your source directory and you want to compile a Java file and put the class in the classes directory:
javac -d ../classes -classpath c:\foo\bar\library1.jar;c:\foo\bar\library2.jar com/foo/bar/HelloWorld.java
Now to run your class:
java -classpath ..\classes;c:\foo\bar\library1.jar;c:\foo\bar\library2.jar com.foo.bar.HelloWorld
When your program becomes bigger that 2 or three classes, you'll probably want to use a build tool like ant, gradle or maven to build your application.
精彩评论