简体   繁体   English

在Eclipse(App Engine)项目中包含JAR文件

[英]Including JAR files in Eclipse (App Engine) project

I have been searching for a solution for this issue for multiple hours today and many hours yesterday so I decided to address it here, although it seems a stupid issue. 我一直在寻找这个问题的解决方案今天和昨天的几个小时,所以我决定在这里解决它,虽然这似乎是一个愚蠢的问题。

Situation: I have a Google AppEngine project setup in Eclipse Java EE. 情况:我在Eclipse Java EE中设置了一个Google AppEngine项目。 It's there for multiple weeks everything runs fine, I can deploy to App Engine and test in Eclipse without probs. 它可以在一周内运行良好,我可以部署到App Engine并在没有probs的情况下在Eclipse中进行测试。

Now I need to add JAR packages for fe JSON support and Google Visualization API. 现在我需要为fe JSON支持和Google Visualization API添加JAR包。 I tried JSON a week ago and because it failed, I just downloaded the source files and added them to my own source. 我一周前尝试过JSON,因为它失败了,我只是下载了源文件并将它们添加到我自己的源代码中。 But now with the Visualizations thing, it are too much source files so I need the JARs working. 但现在使用可视化的东西,它是太多的源文件,所以我需要JAR工作。

What I did: 我做了什么:

  • downloaded the JAR files. 下载了JAR文件。
  • putted them in a PROJECT/lib folder. 将它们放在PROJECT / lib文件夹中。
  • selected them and right-clicked and clicked Build Path > Add to build path. 选择它们并右键单击并单击“构建路径”>“添加到构建路径”。

What happened: All went fine, I could import the classes and create subclasses from them without having errors. 发生了什么:一切顺利,我可以导入类并从中创建子类而没有错误。 So it's clear that Eclipse recognized the classes' existence and Eclipse had them imported successfully. 所以很明显,Eclipse认识到了类的存在,Eclipse已成功导入它们。

Then I tried to build it (Debug Mode) and got following error (the one I got with JSON too): 然后我尝试构建它(调试模式)并得到以下错误(我用JSON得到的那个):

Nov 27, 2011 11:57:48 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: failed chartDataServlet: java.lang.NoClassDefFoundError: com/google/visualization/datasource/DataSourceServlet
Nov 27, 2011 11:57:48 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: failed com.google.appengine.tools.development.DevAppEngineWebAppContext@727db937{/,/Volumes/Data/eclipse_workspace/kulStats/war}: java.lang.NoClassDefFoundError: com/google/visualization/datasource/DataSourceServlet
Nov 27, 2011 11:57:48 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: failed JettyContainerService$ApiProxyHandler@409bad4f: java.lang.NoClassDefFoundError: com/google/visualization/datasource/DataSourceServlet
Nov 27, 2011 11:57:48 AM com.google.apphosting.utils.jetty.JettyLogger warn
WARNING: Error starting handlers
java.lang.NoClassDefFoundError: com/google/visualization/datasource/DataSourceServlet
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:176)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.mortbay.util.Loader.loadClass(Loader.java:91)
at org.mortbay.util.Loader.loadClass(Loader.java:71)
at org.mortbay.jetty.servlet.Holder.doStart(Holder.java:73)
at org.mortbay.jetty.servlet.ServletHolder.doStart(ServletHolder.java:242)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:685)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.jetty.Server.doStart(Server.java:224)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.google.appengine.tools.development.JettyContainerService.startContainer(JettyContainerService.java:191)
at com.google.appengine.tools.development.AbstractContainerService.startup(AbstractContainerService.java:239)
at com.google.appengine.tools.development.DevAppServerImpl.start(DevAppServerImpl.java:146)
at com.google.appengine.tools.development.DevAppServerMain$StartAction.apply(DevAppServerMain.java:164)
at com.google.appengine.tools.util.Parser$ParseResult.applyArgs(Parser.java:48)
at com.google.appengine.tools.development.DevAppServerMain.<init>(DevAppServerMain.java:113)
at com.google.appengine.tools.development.DevAppServerMain.main(DevAppServerMain.java:89)

Caused by: java.lang.ClassNotFoundException: com.google.visualization.datasource.DataSourceServlet
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:176)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 35 more

I don't know if the fact that it prints out the class with slashed instead of with dots is part of the issue: com/google/visualization/datasource/DataSourceServlet . 我不知道它是否用斜线而不是点打印出来的这个事实是问题的一部分: com/google/visualization/datasource/DataSourceServlet In the second part he does use dots. 在第二部分,他确实使用点。

I looked for an issue a very long time, and heard things about classpath file. 我找了很长时间的问题,并听说了关于classpath文件的事情。 My .classpath file looks like this: 我的.classpath文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="src" output="test-classes" path="test"/>
    <classpathentry kind="con" path="com.google.appengine.eclipse.core.GAE_CONTAINER"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="con" path="com.google.appengine.eclipse.core.GAE_CONTAINER/App Engine (1)"/>
    <classpathentry kind="lib" path="war/WEB-INF/lib/chart-datasource/lib/commons-lang-2.4.jar"/>
    <classpathentry kind="lib" path="war/WEB-INF/lib/chart-datasource/lib/commons-logging-1.1.1.jar"/>
    <classpathentry kind="lib" path="war/WEB-INF/lib/chart-datasource/lib/opencsv-1.8.jar"/>
    <classpathentry kind="lib" path="war/WEB-INF/lib/chart-datasource/visualization-datasource-1.1.1.jar"/>
    <classpathentry kind="output" path="war/WEB-INF/classes"/>
</classpath>

Only the war/WEB-INF/classes does not exist.. 只有war/WEB-INF/classes不存在..

Ow, another thing: I found documents about App Engine specifically and they sometimes mentioned the war/WEB-INF/lib folder, so I tried putting it there as well. Ow,另一件事:我专门找到了关于App Engine的文档,他们有时会提到war/WEB-INF/lib文件夹,所以我也尝试将它放在那里。 Of course after deleting from build path and reading after the move. 当然,从构建路径中删除并在移动后读取。 (It's that version of the classpath file I've pasted). (这是我粘贴的那个版本的类路径文件)。

Does anyone have an idea how this can be solved? 有谁知道如何解决这个问题? Summary: Eclipse recognizes the imported JARs, but build fails for some reason... 简介:Eclipse识别导入的JAR,但由于某种原因构建失败...

EDIT: solution I found the following: 编辑:解决方案我发现以下内容:

  • JARs should be in WEB-INF/lib JAR应该在WEB-INF / lib中
  • JARs should not be in folders within lib, all in the root WEB-INF/lib folder. JAR不应位于lib中的文件夹中,所有文件夹都位于根WEB-INF / lib文件夹中。 I had mine in a separate folder and that didn't work. 我有一个单独的文件夹,但没有用。
  • I did Source > Clean Up and Source > Manage imports and it worked. 我做了源>清理和源>管理导入,它工作。

You are on the right track. 你走在正确的轨道上。 Since the app when deployed to appengine is packaged as a war (web application archive), library jars need to be in WEB-INF/lib. 由于部署到appengine的应用程序打包为战争(Web应用程序存档),因此库jar需要位于WEB-INF / lib中。 You should ensure your jars are there and then add those jars to your eclipse build path. 你应该确保你的罐子在那里,然后将这些罐子添加到你的eclipse构建路径中。 Try doing a "clean" operation on the project and re-building it to ensure the libraries is in the package. 尝试对项目执行“干净”操作并重新构建它以确保库位于包中。

this solution that: 这个解决方案:

  • copy source (.jar file) into war/WEB-INF/lib folder 将源(.jar文件)复制到war / WEB-INF / lib文件夹中

您仍然需要将jar文件添加到java构建路径,以避免在将文件添加到war / WEB-INF / lib文件夹后出现编译错误!

Copy paste the .jar files to war/WEB-INF/lib and then go to Properties(of the project in Eclipse) --> Java Build Path --> Add JARs --> Select the related .jars from the current project. 复制将.jar文件粘贴到war / WEB-INF / lib,然后转到Properties(Eclipse中的项目) - > Java Build Path - > Add JARs - >从当前项目中选择相关的.jars。 You should be all set! 你应该全力以赴!

Might be "a little" too late for this, but in eclipse you don't have to manually copy. 这可能是“有点”太迟了,但在日食中你不必手动复制。 Right-Click on project -> Build Path -> Configure Build Path... -> select Deployment Assembly (left). 右键单击项目 - >构建路径 - >配置构建路径... - >选择部署程序集(左)。 Add your jars here and they will be included when deploying. 在这里添加你的罐子,它们将在部署时包括在内。

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

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