How to import libraries from JAR-files into a Java program using TextMate

你说的曾经没有我的故事 提交于 2019-12-22 13:06:29

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6556452/how-to-import-libraries-from-jar-files-into-a-java-program-using-textmate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!