本文介绍了基于APR的Apache Tomcat Native库在java.library.path中找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在服务器开发中,从
Lars Vogel的简单教程开始。



一步一步 符合本教程:




    安装Eclipse Java EE开普勒;
  • 在Ubuntu 12.04上安装tomcat 7 - http:// localhost:8080 / 显示正确tomcat页面;

  • 将tomcat运行环境设置为eclipse;

  • 将tomcat服务器添加到eclipse;

  • 创建 DAO ;

  • 创建Servlet;

  • run =>



这里我抓到下一个提示:

  Sep 15,2013 3:40:39 PM org.apache.catalina.core.AprLifecycleListener init 
INFO:基于APR的Apache Tomcat Native库,在生产环境中允许最佳性能在java.library.path中找不到: / usr / java / packages / lib / amd64:/ usr / lib64:/ lib64:/ lib:/ usr / lib
2013年9月15日3:40:42 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
警告:[SetPropertiesRule] {Server / Service / Engine / Host / Context}将属性'source'设置为'org.eclipse.jst .jee.server:com.filecounter'没有找到匹配的属性。
2013年9月15日3:40:43 PM org.apache.coyote.AbstractProtocol init
INFO:初始化ProtocolHandler [http-bio-8080]
2013年9月15日3:40 :43 PM org.apache.coyote.AbstractProtocol init
INFO:初始化ProtocolHandler [ajp-bio-8009]
2013年9月15日3:40:43 PM org.apache.catalina.startup。 Catalina加载
INFO:初始化处理5203毫秒
2013年9月15日3:40:43 PM org.apache.catalina.core.StandardService startInternal
信息:启动服务Catalina
2013年9月15日3:40:43 PM org.apache.catalina.core.StandardEngine startInternal
INFO:启动Servlet引擎:Apache Tomcat / 7.0.42
2013年9月15日3:40:45 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO:使用[SHA1PRNG]创建用于生成会话ID的SecureRandom实例花费了[171]毫秒。
2013年9月15日3:40:46 PM org.apache.coyote.AbstractProtocol start
信息:启动ProtocolHandler [http-bio-8080]
2013年9月15日3:40 :46 PM org.apache.coyote.AbstractProtocol start
INFO:启动ProtocolHandler [ajp-bio-8009]
2013年9月15日3:40:46 PM org.apache.catalina.startup。 Catalina开始
INFO:服务器启动在2882毫秒

这里是 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:5 9 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
/ pre>



更新:



这是教程:



Update2:



以下是数据访问对象的内容:

  public class FileDao {

public int getCount(){
int count = 0;
//使用计数器加载文件
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();
}
}
返回计数;
}

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

//确保关闭文件
if(printWriter!= null){
printWriter.close();
}
}

}

此处Servlet代码:

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

int count;
私人FileDao dao;

public void init()throws ServletException {
dao = new FileDao();
try {
count = dao.getCount();
} catch(Exception e){
getServletContext()。log(FileCounter中出现异常,e);
抛出新的ServletException(FileCounter中出现异常
+ e.getMessage());
}
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException,IOException {
//为用户设置一个cookie,所以计数器不会增加
//每次用户刷新
HttpSession session = request.getSession(true);
//设置会话有效期为5秒
session.setMaxInactiveInterval(5);
response.setContentType(text / plain);
PrintWriter out = response.getWriter();
if(session.isNew()){
count ++;
}
out.println(此站点已被访问+ count +次。
}

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

}

我避风港没有 web.xml



如何解决这个麻烦? em>

解决方案

预计在以下位置之一

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

不在

  tomcat / lib 

tomcat / lib 中的文件都是 jar 文件,并由tomcat添加到 classpath ,以便它们可用于您的应用程序。



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

只需,并将其放在tomcat正在期待的位置之一。



请注意,您不需要将此lib用于开发/测试目的。

编辑



您收到的输出非常正常,它只是一些tomcat的日志输出,上面的行表示服务器正确启动并准备好运行。



如果您在运行您的servlet时遇到麻烦,在服务器上运行命令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-class url-mapping 。它可能是这样的:

 < servlet> 
< description>< / description>
< display-name>文件计数器 - 我的第一个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 
/ pre>

也可以。


I'm newly at server development and have been started from easy tutorial by Lars Vogel. Servlet and JSP development with Eclipse WTP .

Step by step accord this tutorial:

  • installed Eclipse Java EE Kepler;
  • installed tomcat 7 on Ubuntu 12.04 - http://localhost:8080/ shows correct tomcat page;
  • Setting up tomcat runtime environments into eclipse;
  • added tomcat server to eclipse;
  • create DAO;
  • created the Servlet;
  • run =>

And here I caught next prompt:

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

Here is content of tomcat/lib folder:

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.

Update:

Here is tutorial:
Installing Apache Tomcat Native on Linux Ubuntu 12.04

Update2:

Here is content of Data Access Object:

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();
    }
  }

} 

And here Servlet code:

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();
    }
  }

} 

I haven't had web.xml yet.

How to solve this trouble?

解决方案

The native lib is expected in one of the following locations

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

and not in

tomcat/lib

The files in tomcat/lib are all jar file and are added by tomcat to the classpath so that they are available to your application.

The native lib is needed by tomcat to perform better on the platform it is installed on and thus cannot be a jar, for linux it could be a .so file, for windows it could be a .dll file.

Just download the native library for your platform and place it in the one of the locations tomcat is expecting it to be.

Note that you are not required to have this lib for development/test purposes. Tomcat runs just fine without it.

EDIT

The output you are getting is very normal, it's just some logging outputs from tomcat, the line right above indicates that the server correctly started and is ready for operating.

If you are troubling with running your servlet then after the run on sever command eclipse opens a browser window (embeded (default) or external, depends on your config). If nothing shows on the browser, then check the url bar of the browser to see whether your servlet was requested or not.

It should be something like that

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

EDIT 2

Try to call your servlet using the following url

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

Also each web project has a web.xml, you can find it in your project under WebContent\WEB-INF.

It is better to configure your servlets there using servlet-name servlet-class and url-mapping. It could look like that:

  <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>

In eclipse dynamic web project the default context name is the same as your project name.

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

will work too.

这篇关于基于APR的Apache Tomcat Native库在java.library.path中找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 16:33