java 在 ant 编译中包含 JAR 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5106749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Include JAR file in ant compilation
提问by TheFrancisOne
I want to compile some .java
files into a JAR. I would like to use this JAR file in another application.
我想将一些.java
文件编译成 JAR。我想在另一个应用程序中使用这个 JAR 文件。
Is this a good way to use functions from my first application in my second application? My goal is to not duplicate code.
这是在我的第二个应用程序中使用我的第一个应用程序中的函数的好方法吗?我的目标是不重复代码。
How can I edit the build.xml
file for the second application to include the JAR file I created? When I compile my project, functions from JAR files are not resolved :
如何编辑build.xml
第二个应用程序的文件以包含我创建的 JAR 文件?当我编译我的项目时,JAR 文件中的函数没有解析:
[javac] C:\HelloWorld\src\com\example\helloworld\HelloWorld.java:7:
package TOOLS does not exist
[javac] import TOOLS.Logs;
EDIT:
When I add the javac
task in my build.xml
, compilation fails because other packages are not found:
编辑:
当我javac
在我的任务中添加任务时build.xml
,编译失败,因为找不到其他包:
[javac] Compiling 1 source file
[javac] C:\HelloWorld\src\com\example\hellowolrd\HelloWorld.java:3:
package android.app does not exist
[javac] import android.app.Activity;
[javac] ^
Why are my original packages not found after I include the JAR file?
为什么在我包含 JAR 文件后找不到我的原始包?
回答by Brian Agnew
Yes. It's good practice generally to subdivide functionality into libraries for reuse, ease-of-development etc. It'll speed development if they're separately built, and you can easily distribute functionality to successive projects.
是的。通常将功能细分为库以便重用、易于开发等是一种很好的做法。如果单独构建它们会加快开发速度,并且您可以轻松地将功能分发到连续的项目中。
You can reference your jar thus:
您可以这样引用您的 jar:
<javac srcdir="${src}"
destdir="${build}"
classpath="xyz.jar"
debug="on"
/>
See the javacdocumentation for more information.
有关更多信息,请参阅javac文档。
回答by J.N.
回答by JB Nizet
Use the classpath or classpathref attribute in the javac task, or include a nested classpath element, as described in the javac ant task documentation.
在 javac 任务中使用 classpath 或 classpathref 属性,或包含嵌套的 classpath 元素,如javac ant 任务文档中所述。
Note : packages in Java should be in lowercase.
注意:Java 中的包应该是小写的。