java - 在 java.library.path 上找不到基于 APR 的 Apache Tomcat native 库

标签 java eclipse tomcat servlets

我是服务器开发的新手,从简单教程开始 拉斯沃格尔。 Servlet and JSP development with Eclipse WTP .

一步一步按照本教程:

  • 安装了 Eclipse Java EE Kepler;
  • 在 Ubuntu 12.04 上安装了 tomcat 7 - http://localhost:8080/ 显示正确的 tomcat 页面;
  • 在eclipse中设置tomcat运行环境;
  • 将tomcat服务器添加到eclipse;
  • 创建DAO;
  • 创建了 Servlet;
  • 运行 =>

在这里我捕捉到了下一个提示:

Sep 15, 2013 3:40:39 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 15, 2013 3:40:42 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:com.filecounter' did not find a matching property.
Sep 15, 2013 3:40:43 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Sep 15, 2013 3:40:43 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Sep 15, 2013 3:40:43 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 5203 ms
Sep 15, 2013 3:40:43 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 15, 2013 3:40:43 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.42
Sep 15, 2013 3:40:45 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [171] milliseconds.
Sep 15, 2013 3:40:46 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Sep 15, 2013 3:40:46 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Sep 15, 2013 3:40:46 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2882 ms

这是 tomcat/lib 文件夹的内容:

nazar_art@nazar-desctop:/usr/local/tomcat/apache-tomcat-7.0.42/lib$ ls -lg
total 6132
-rwxrwxrwx 1 nazar_art   15264 Jul  2 10:59 annotations-api.jar
-rwxrwxrwx 1 nazar_art   54142 Jul  2 10:59 catalina-ant.jar
-rwxrwxrwx 1 nazar_art  134215 Jul  2 10:59 catalina-ha.jar
-rwxrwxrwx 1 nazar_art 1581311 Jul  2 10:59 catalina.jar
-rwxrwxrwx 1 nazar_art  257520 Jul  2 10:59 catalina-tribes.jar
-rwxrwxrwx 1 nazar_art 1801636 Jul  2 10:59 ecj-4.2.2.jar
-rwxrwxrwx 1 nazar_art   46085 Jul  2 10:59 el-api.jar
-rwxrwxrwx 1 nazar_art  123241 Jul  2 10:59 jasper-el.jar
-rwxrwxrwx 1 nazar_art  599428 Jul  2 10:59 jasper.jar
-rwxrwxrwx 1 nazar_art   88690 Jul  2 10:59 jsp-api.jar
-rwxrwxrwx 1 nazar_art  177598 Jul  2 10:59 servlet-api.jar
-rwxrwxrwx 1 nazar_art    6873 Jul  2 10:59 tomcat-api.jar
-rwxrwxrwx 1 nazar_art  796527 Jul  2 10:59 tomcat-coyote.jar
-rwxrwxrwx 1 nazar_art  235411 Jul  2 10:59 tomcat-dbcp.jar
-rwxrwxrwx 1 nazar_art   77364 Jul  2 10:59 tomcat-i18n-es.jar
-rwxrwxrwx 1 nazar_art   48693 Jul  2 10:59 tomcat-i18n-fr.jar
-rwxrwxrwx 1 nazar_art   51678 Jul  2 10:59 tomcat-i18n-ja.jar
-rwxrwxrwx 1 nazar_art  124006 Jul  2 10:59 tomcat-jdbc.jar
-rwxrwxrwx 1 nazar_art   23201 Jul  2 10:59 tomcat-util.jar

Here is content of catalina.2013-09-15.log .

更新:

这里是教程:
Installing Apache Tomcat Native on Linux Ubuntu 12.04

更新2:

这是数据访问对象的内容:

public class FileDao {

  public int getCount() {
    int count = 0;
    // Load the file with the counter
    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    PrintWriter writer = null ; 
    try {
      File f = new File("FileCounter.initial");
      if (!f.exists()) {
        f.createNewFile();
        writer = new PrintWriter(new FileWriter(f));
        writer.println(0);
      }
      if (writer !=null){
        writer.close();
      }

      fileReader = new FileReader(f);
      bufferedReader = new BufferedReader(fileReader);
      String initial = bufferedReader.readLine();
      count = Integer.parseInt(initial);
    } catch (Exception ex) {
      if (writer !=null){
        writer.close();
      }
    }
    if (bufferedReader != null) {
      try {
        bufferedReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return count;
  }

  public void save(int count) throws Exception {
    FileWriter fileWriter = null;
    PrintWriter printWriter = null;
    fileWriter = new FileWriter("FileCounter.initial");
    printWriter = new PrintWriter(fileWriter);
    printWriter.println(count);

    // Make sure to close the file
    if (printWriter != null) {
      printWriter.close();
    }
  }

} 

这里是 Servlet 代码:

public class FileCounter extends HttpServlet {
  private static final long serialVersionUID = 1L;

  int count;
  private FileDao dao;

  public void init() throws ServletException {
    dao = new FileDao();
    try {
      count = dao.getCount();
    } catch (Exception e) {
      getServletContext().log("An exception occurred in FileCounter", e);
      throw new ServletException("An exception occurred in FileCounter"
          + e.getMessage());
    }
  }

  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    // Set a cookie for the user, so that the counter does not increate
    // every time the user press refresh
    HttpSession session = request.getSession(true);
    // Set the session valid for 5 secs
    session.setMaxInactiveInterval(5);
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();
    if (session.isNew()) {
      count++;
    }
    out.println("This site has been accessed " + count + " times.");
  }

  public void destroy() {
    super.destroy();
    try {
      dao.save(count);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

} 

我还没有web.xml

如何解决这个问题?

最佳答案

not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

native 库应位于以下位置之一

/usr/java/packages/lib/amd64
/usr/lib64
/lib64
/lib
/usr/lib

而不是在

tomcat/lib

tomcat/lib 中的文件都是jar 文件,由tomcat 添加到classpath 以便你的应用可以使用.

tomcat 需要本地库才能在其安装的平台上更好地执行,因此不能是 jar,对于 linux,它可以是 .so 文件, 对于 Windows,它可能是一个 .dll 文件。

只需 download the native library for your platform并将其放置在 tomcat 期望的位置之一。

请注意,出于开发/测试目的,您不需要拥有此库。没有它,Tomcat 也能正常运行。

org.apache.catalina.startup.Catalina start INFO: Server startup in 2882 ms

编辑

你得到的输出很正常,只是tomcat的一些日志输出,上面的那行表示服务器正确启动,可以运行了。

如果您在运行 servlet 时遇到问题,那么在 run on sever 命令之后,eclipse 会打开一个浏览器窗口(嵌入(默认)或外部,取决于您的配置)。如果浏览器上没有显示任何内容,请检查浏览器的 url 栏以查看您的 servlet 是否被请求。

应该是这样的

http://localhost:8080/<your-context-name>/<your-servlet-name>

编辑 2

尝试使用以下 url 调用您的 servlet

http://localhost:8080/com.filecounter/FileCounter

另外每个 web 项目都有一个 web.xml,你可以在你的项目中的 WebContent\WEB-INF 下找到它。

最好使用 servlet-name servlet-classurl-mapping 配置您的 servlet。它可能看起来像这样:

  <servlet>
    <description></description>
    <display-name>File counter - My first servlet</display-name>
    <servlet-name>file_counter</servlet-name>
    <servlet-class>com.filecounter.FileCounter</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>file_counter</servlet-name>
    <url-pattern>/FileFounter</url-pattern>
  </servlet-mapping>

在 Eclipse 动态 Web 项目中,默认上下文名称与您的项目名称相同。

http://localhost:8080/<your-context-name>/FileCounter

也可以。

关于java - 在 java.library.path 上找不到基于 APR 的 Apache Tomcat native 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18812762/

相关文章:

java - NFC 权限错误

java - 带有 TOMCAT 的 Windows 2008 服务器的 Amazon ec2

tomcat - PermGen 问题 - Groovy GString 实习生字符串吗?

java - 这段代码中的while循环如何实现?

java - 如何删除parse.com类中数组类型对象中的值

java - 检查 ArrayList 是否包含另一个 ArrayList 中的所有字符串

java - Eclipse 自动关闭

java - Android:Eclipse jar 图标为白色且无法访问类

java - 如何使用 @JsonIgnoreProperties 忽略 JSON 响应上的特定子属性?

iphone - 当服务器在谷歌云上时,websocket 在 iPad、iPhone 上失败