Include JAR file in ant compilation

心已入冬 提交于 2019-12-10 21:57:22

问题


I want to compile some .java files into a JAR. I would like to use this JAR file in another application.

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 :

[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] 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?


回答1:


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:

  <javac srcdir="${src}"
         destdir="${build}"
         classpath="xyz.jar"
         debug="on"
  />

See the javac documentation for more information.




回答2:


You need to add your JAR to your classpath.

 <javac srcdir="${src}"
         destdir="${build}"
         classpath="xyz.jar"
         debug="on"
         source="1.4"
  />

See here for more info.




回答3:


Use the classpath or classpathref attribute in the javac task, or include a nested classpath element, as described in the javac ant task documentation.

Note : packages in Java should be in lowercase.



来源:https://stackoverflow.com/questions/5106749/include-jar-file-in-ant-compilation

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