Java 代码在 App 中运行,而不是在 Servlet 中运行

发布于 2024-12-21 00:34:19 字数 1591 浏览 0 评论 0原文

情况是这样的。

我正在 Java Web 应用程序上实现面部识别软件,其中包括两个 .jar 文件和一个 .dylib 本机库。在普通的 java 应用程序中,代码工作正常,但是当我尝试在 servlet 中加载库时,出现找不到 .dylib 的错误。如何让 servlet 加载库以便我可以访问面部识别软件?

这是我的代码。有效的 Java 应用程序。

public static void main(String[] args) {

    FSDK.ActivateLibrary("G2TfLOGUH8hQehjxiB...");
    FSDK.Initialize();
    FSDK.Finalize();
}

这是 servlet。

@WebServlet(name = "glassesServlet", urlPatterns = {"/glassesServlet"})
public class glassesServlet extends HttpServlet {

/** 
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {

    FSDK.ActivateLibrary("G2TfLOGUH8hQehjxiBZtJvjmwyu...");
    FSDK.Initialize();
    FSDK.Finalize();

    } finally {            
        out.close();
    }
}

现在,这是每次我尝试从服务器运行时服务器给出的错误。

WARNING: StandardWrapperValve[glassesServlet]: PWC1406: Servlet.service() for servlet         
java.lang.NoClassDefFoundError: Could not initialize class Luxand.FSDK$IFaceSDK

加上一些其他详细说明其起源的代码。该类 Luxand.FSDK$IFaceSDK 是存储在 Library 文件夹中的 .jar 文件内的一个类。

任何帮助都会很棒,谢谢!

Here's the situation.

I'm implementing facial recognition software on a Java Web Application, which includes two .jar files and a .dylib native library. In a normal java application, the code works fine, however when I try to load the library in a servlet, I'm getting the error that the .dylib can't be found. How can I get the servlet to load the library so that I can access the facial recognition software?

Here's my code. The Java App that works.

public static void main(String[] args) {

    FSDK.ActivateLibrary("G2TfLOGUH8hQehjxiB...");
    FSDK.Initialize();
    FSDK.Finalize();
}

Here's the servlet.

@WebServlet(name = "glassesServlet", urlPatterns = {"/glassesServlet"})
public class glassesServlet extends HttpServlet {

/** 
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {

    FSDK.ActivateLibrary("G2TfLOGUH8hQehjxiBZtJvjmwyu...");
    FSDK.Initialize();
    FSDK.Finalize();

    } finally {            
        out.close();
    }
}

Now, here's the error that the server gives each time I try to run from the server.

WARNING: StandardWrapperValve[glassesServlet]: PWC1406: Servlet.service() for servlet         
java.lang.NoClassDefFoundError: Could not initialize class Luxand.FSDK$IFaceSDK

plus some other code that details where it originated. That class, Luxand.FSDK$IFaceSDK is a class inside the .jar file that is stored in the Library folder.

Any help would be great, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷离° 2024-12-28 00:34:19

确保库文件夹位于部署环境的类路径上

make sure the library folder is on classpath of deployment environment

月下客 2024-12-28 00:34:19

上面两个答案都是对的。提供更多细节...

在您的笔记本电脑上,它可能只是从项目的根目录中获取 .dylib 文件。

在 Linux 上,您需要 .so 文件。只要位于 LD_LIBRARY_PATH 环境变量路径上即可,它在哪里并不重要。我发现最简单的方法是在启动服务器的命令行上进行设置。

所以如果你使用的是 fat JAR,它会是这样的:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/luxand/luxand-something.so java -jar Server.jar -Dsomeothersetting=something

Both of the above answers are right. To give a little more detail...

On your laptop it's probably just picking up the .dylib file from the root of your project.

On Linux, you need the .so file. It doesn't matter where it is as long as it is on the LD_LIBRARY_PATH environment variable path. I've found the easiest way is to set that on the command-line launching the server.

So if you're using a fat JAR, it would be something like:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/luxand/luxand-something.so java -jar Server.jar -Dsomeothersetting=something
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文