简体   繁体   English

如何链接用于Java类路径的库?

[英]How to link libraries used to java classpath?

In Eclipse (Windows 7) I created a dynamic web project. 在Eclipse(Windows 7)中,我创建了一个动态Web项目。 My classes use many external libraries. 我的课程使用许多外部库。 These were copied into the directory WebContent/WEB-INF/lib/ manually and will be included in the resulting WAR file on building (right-click/Export/...). 它们被手动复制到WebContent/WEB-INF/lib/目录中,并将包含在生成的生成的WAR文件中(右键单击/导出/ ...)。 The WAR file will be deployed on a Tomcat 6 running on a Ubuntu server. 该WAR文件将部署在Ubuntu服务器上运行的Tomcat 6上。

My question is if it's possible to "to tell" the WAR file which packages to use at runtime. 我的问题是,是否有可能“告诉” WAR文件在运行时使用哪些软件包。 Maybe with running the build command manually with parameters added? 也许通过手动运行build命令并添加了参数? Or is this the wrong directory the files copied in? 还是这是文件复制到的目录错误?

I tried out the following: All external JARs were copied into $JAVA_HOME/jre/lib/ext , so they should appear automatically in classpath. 我尝试了以下操作:将所有外部JAR复制到$JAVA_HOME/jre/lib/ext ,因此它们应自动出现在classpath中。 Before exporting and deploying the WAR file I deleted all JARs in WebContent/WEB-INF/lib/ . 在导出和部署WAR文件之前,我删除了WebContent/WEB-INF/lib/所有JAR。 But that results in a ClassNotFound exception on server-side. 但这会在服务器端导致ClassNotFound异常。

When working with wars, the general idea is to create a war that can be deployed to any instance of Tomcat without having to modify the host environment, which you can't always do due to permissions, access, desired up time, etc. 使用war时,通常的想法是创建一个可以部署到任何Tomcat实例而无需修改主机环境的war,由于权限,访问,所需的正常运行时间等原因,您无法总是这样做。

That said, any libraries your app depends on should appear in the war, in WEB-INF/lib . 就是说,您的应用程序所依赖的任何库都应该出现在战争中WEB-INF/lib

As you've noticed, maintaining the jars can be a pain. 如您所知,维护罐子可能会很痛苦。 Look into using a build tool such as Maven or at least Ant. 研究使用Maven或至少Ant等构建工具。

Also, jars in the Java environment and jars accessible to apps running on Tomcat are two different sets. 另外,Java环境中的jar和Tomcat上运行的应用程序可访问的jar是两个不同的集合。 Tomcat doesn't give access to the underlying Java system for apps running under itself. Tomcat不允许其下运行的应用程序访问底层Java系统。 You could put the jars you need in TOMCAT_HOME\\lib, but that's not really recommended for previously stated reasons. 可以将所需的罐子放在TOMCAT_HOME \\ lib中,但是出于先前所述的原因,我们不建议这样做。

For exclude the libs you can use war task 对于排除库,您可以使用战争任务

I can image you can right click your eclipse project then export -> select ant Buildfiles 我可以映像您可以右键单击您的Eclipse项目,然后导出->选择ant Buildfiles

add the code from above link, an example, like this, case by case please specify your html, images, output classes folder, and so on. 从上面的链接添加代码,一个例子,像这样,视情况而定,请指定您的html,图像,输出类文件夹等。

<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
  <fileset dir="src/html/myapp"/> 
  <fileset dir="src/jsp/myapp"/>
  <lib dir="thirdparty/libs">
    <exclude name="jdbc1.jar"/> // add more lib in your case
  </lib>
  <classes dir="build/main"/> // the class path would be ./build/classes
  <zipfileset dir="src/graphics/images/gifs"
              prefix="images"/>
</war>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM