如何使用TextMate将库从JAR文件导入Java程序 [英] How to import libraries from JAR-files into a Java program using TextMate
问题描述
我需要用Java编写一个简单的程序,并且喜欢这样做,就像我使用TextMate在Python和Ruby中编程一样。我可以编写并运行一个简单的Hello World程序,但是找不到如何加载外部库的方法。我只需要使用工作目录中的所有* .jar文件进行编译(或者我是否需要首先安装它们?)。这是我的第一次Java体验,所以我希望得到一个详细的答案。
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.
我更喜欢在TextMate中编写代码而不是IDE,所以我想知道这是否可行Java。
I do prefer to code in TextMate not an IDE, so I wonder if this is possible with Java.
推荐答案
为了能够在程序中使用存储在jar文件中的类,这个jar文件必须是类路径。
To be able to use classes stored inside a jar file in your program, this jar file must be part of the classpath.
阅读 http://en.wikipedia.org / wiki / 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.
假设您位于源目录的根目录并且想要编译Java文件并将该类放入classes目录:
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
现在运行你的类:
java -classpath ..\classes;c:\foo\bar\library1.jar;c:\foo\bar\library2.jar com.foo.bar.HelloWorld
当你的程序变得比2或3个类更大时,你可能想要使用像ant,gradle或maven这样的构建工具来构建你的应用程序。
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.
这篇关于如何使用TextMate将库从JAR文件导入Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!