简体   繁体   English

如何在不使用IDE的情况下在Java中包含库

[英]how to include libraries in java without using an IDE

How do I import libraries in my java program without using an IDE like Netbeans? 如何在不使用Netbeans等IDE的情况下在Java程序中导入库? In Netbeans I do it this way: 在Netbeans中,我这样做: 在此处输入图片说明

How can I achieve the same thing by just using notepad++ or programmer's notepad. 我如何仅通过使用notepad ++或程序员的记事本来实现相同的目的。 As much as possible I don't want to use Netbeans because it would be overkill since I'm only working on simple projects. 我不想使用Netbeans,因为我只从事简单的项目,所以这可能会过分杀人。

javac -classpath external.jar myClass.java

EDIT: 编辑:

If your main class is in a package 如果您的主要班级在包中

package com.mycompany;
public class myClass
{
...
...

then 然后

you'll need 你需要

javac -classpath external.jar com/mycompany/myClass.java 

and

to run 跑步

java -classpath external.jar com.mycompany.myClass

In addition to @StackOverflowException's post adding multiple files and locations is prefectly ok too... 除了@StackOverflowException的帖子,添加多个文件和位置也完全可以...

javac -cp location1/;location2/;file1.jar;file2.jar fileToCompile

Notes:: 笔记::

-cp and -classpath are the same thing. -cp和-classpath是同一件事。 If you're on Solaris (and some other UNIX flavors) change the ';' 如果您使用的是Solaris(以及其他一些UNIX版本),请更改“;” to ':' 至 ':'

All of the other posters are spot on, you just need to add the jar to your classpath. 所有其他海报都可以找到,只需将罐子添加到类路径中即可。

Java offers many mechanisms for setting the classpath, including via the command line, via an environment variable, and through setting it in the MANIFEST.MF of an executable Java jar file. Java提供了许多用于设置类路径的机制,包括通过命令行,环境变量以及在可执行Java jar文件的MANIFEST.MF中进行设置。

These are all a pain in the neck to manage. 这些都是要处理的脖子上的痛苦。 It's good to know the technique, and understand the basics. 了解技术并了解基础知识是件好事。 But it's really a bad idea to actually use them. 但是,实际使用它们确实不是一个好主意。

What you should do is this. 您应该做的是这个。

First, put all of your Java libraries in a single place on your system. 首先,将所有Java库放在系统中的单个位置。 C:\\java\\libraries, or whatever. C:\\ java \\ libraries或其他。 Someplace that you remember, someplace accessible by all of your projects. 您记得的某个地方,所有项目都可以访问的某个地方。

Next, name all of your libraries using their version numbers. 接下来,使用其版本号命名所有库。 If you using log4j v1.4.1, then put the jar in a log4j-1.4.1 directory in your library area. 如果使用log4j v1.4.1,则将jar放在库区域的log4j-1.4.1目录中。 This gives you "free" library versioning. 这为您提供了“免费”的库版本控制。

Finally, learn Ant. 最后,学习蚂蚁。 For simple projects, Ant is simple. 对于简单的项目,Ant很简单。 Use the Ant build.xml file to compile, test, and run your application. 使用Ant build.xml文件来编译,测试和运行您的应用程序。

Why? 为什么? Several reasons. 几个原因。

Because once it's set up, adding a new library to your project is trivial, you add a line to your build.xml. 因为设置完成后,向您的项目添加新库很简单,所以您可以在build.xml中添加一行。 Ant lets you more easily handle simple abstractions (like where all of your libraries are located). Ant使您可以更轻松地处理简单的抽象(例如所有库的位置)。

The build.xml is self contained. build.xml是自包含的。 If you use, say, an environment variable for the classpath, then the classpath for one project may be different from that of another. 例如,如果为类路径使用环境变量,则一个项目的类路径可能与另一个项目的类路径不同。 That means reseting the environment variable. 这意味着重置环境变量。 Continue this and you'll end up swearing at some "new problem" where it "worked before" when it's because you had your classpath set wrong. 继续进行此操作,您将最终在某些“新问题”面前发誓,因为您的类路径设置错误,因此该问题在“之前”起作用。 Set it once in the build.xml, and forget it. 在build.xml中设置一次,然后忘记它。

Ant is portable. 蚂蚁是便携式的。 It runs the same on Windows, on Linux, on Mac, on AS/400, it runs everywhere that Java runs, unlike shells scripts or BAT files. 它在Windows,Linux,Mac,AS / 400上运行相同,并且在Java运行的所有位置运行,与shell脚本或BAT文件不同。

It's lightweight. 轻巧。 Simple ant scripts are simple. 简单的蚂蚁脚本很简单。 They don't bring a lot of baggage with them, you can always make them scary complicated. 它们不会带来很多负担,您总是可以使它们变得可怕而复杂。 It's much simpler than Maven for just builds. 就构建而言,它比Maven简单得多。

Most IDEs support Ant directly. 大多数IDE直接支持Ant。 If you decided to go back to an IDE, most can simply use your ant build file with minimal configuration. 如果您决定返回到IDE,那么大多数人可以简单地以最少的配置使用您的ant构建文件。

This is how you solve your classpath problem with notepad++. 这就是您使用notepad ++解决类路径问题的方法。 Setting the classpath works, but it doesn't go far enough, it's a pain to administer and manage. 设置类路径是可行的,但是还远远不够,这很难管理。 Learning the basics of Ant will take you much farther with minimal work. 学习Ant的基础知识将以最少的工作使您走得更远。

You should put them on your classpath, like 您应该将它们放在类路径中,例如

java -classpath someJar.jar YourMainClass

And, of course, you can do the same for javac . 而且,当然,您可以对javac执行相同的操作。

If you need to have more than one jar or directory on your classpath, you'll need to use your platform's default path separator. 如果您的类路径上需要有多个jar或目录,则需要使用平台的默认路径分隔符。 For example, on Windows, 例如,在Windows上,

java -classpath someJar.jar;myJar.jar YourMainClass

On a side note, you might find it easier to use an IDE to manage this sort of stuff. 附带一提,您可能会发现使用IDE来管理这种事情比较容易。 I've personally used just my slightly scriptable editor and have managed fine. 我个人只使用了略带脚本的编辑器,并且运行良好。 But it's good to know how to do this stuff by command line. 但是很高兴知道如何通过命令行来完成这些工作。

将罐子放在您的类路径中,类路径是环境变量

确保jar文件位于类路径中,并且具有import语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用IDE的情况下调试Java程序? - How to debug a Java program without using an IDE? 如何在不使用IDE的情况下解决Java中的NoClassDefFoundError - How to solve NoClassDefFoundError in java without using IDE 如何在不使用Maven的情况下排除Java库? - How to exclude java libraries without using Maven? 如何将 lwjgl 3.2.2 库包含到 intelliJ IDE 中? - How do I include the lwjgl 3.2.2 libraries into intelliJ IDE? 如何在Java中包含文件? 在Eclipse IDE中可以吗? - How to include files in java ? Is it possible in Eclipse IDE? 如何在不使用IDE或命令提示符的情况下运行Java应用程序 - How to run Java applications without using an IDE or the command prompt 如何在不使用IDE的情况下在Linux上运行Java项目 - How to Run a Java Project on Linux Without Using IDE 有没有办法像Java Webstart中那样在JAR中包含本机库,而无需使用Webstart? - Is there a way to include native libraries in a JAR, like in Java Webstart, without using Webstart? 如何在多个项目中包含外部库而不进行多个副本 - How to include external libraries in multiple projects without making multiple copies android java Java如何在不使用外部库的情况下从毫秒获取月份 - Java How to get Month from milliseconds WITHOUT using external Libraries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM