3

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 this is possible with Java.

Andrei
  • 10,918
  • 12
  • 76
  • 110

1 Answers1

5

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • JB Nizet, thanks for your answer! In TextMate I can run my HelloWorld program by pressing Cmd+R. I could simply add a Ruby file which will do the routines and provide me the program output and error messages... – Andrei Jul 02 '11 at 11:09