【问题标题】:How to import java project as module in java web project using intelijj?如何使用intellij在java web项目中将java项目作为模块导入?
【发布时间】:2022-01-01 13:15:45
【问题描述】:

我想在另一个项目中定义的 java servlet 类中使用基本 java 项目中的类。 我尝试通过模块依赖 InteliJ 菜单将项目作为模块导入。 在编译时,它没有给出任何错误,但是在运行服务器(Glassfish)并调用 servlet 后,它给出了以下错误。

java.lang.NoClassDefFoundError: com/practise/LogFileCreator at UserLoginValidator.dbConnectionMaker(UserLoginValidator.java:31)>

请在下面找到导致错误的代码。

以下课程来自网络项目

import jakarta.servlet.*;
import jakarta.servlet.http.*;

import java.io.*;

import com.practise.LogFileCreator;

public class UserLoginValidator extends HttpServlet
{
    public String LogFilePath="D:\\Logs";
    public PrintWriter out;
    String errormsg="";
   //********************
    LogFileCreator l ;

    @Override
    public void init() throws ServletException {

            try {
                this.l = new LogFileCreator(LogFilePath); // here i am trying to create object of my class which causing the mentioned error.
                l.WriteLog("Hello");
            } catch (IOException e) {
                e.printStackTrace();
            }
        

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {

    }


}

下面的类来自普通的java项目

package com.practise;
import java.io.*;

public class LogFileCreator
{
    private String filepath;
    private StringBuffer sb = new StringBuffer("Log");
    private File file;
    private FileWriter fileWriter;
    private BufferedWriter bufferedWriter;
    public PrintWriter p;

    public LogFileCreator(String filepath) throws IOException
    {
        this.filepath=filepath;
        String filename=sb.toString().concat(java.time.LocalDate.now().toString());
        this.file = new File(this.filepath,filename);
        this.fileWriter= new FileWriter(file,true);
        if(!file.exists())
        {
            file.createNewFile();
        }
        p= new PrintWriter(fileWriter);

    }

    public void WriteLog(String logMessage){
        p.println(java.time.LocalDateTime.now() + " : " + logMessage);
        p.flush();
    }
}

这是我使用的模块依赖的图像。 Image

之前我使用的是同一个 Web 项目中的 LogFileCreator.java 类,它运行良好

我想要实现的是,无需在 web 项目中再次编写 LogFileCreator 类,想要重用已经在普通 java 项目中编写的类来打印所需文本文件中的日志。

任何解决方案/建议将不胜感激。 谢谢!

[编辑 1] pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>webapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->

    <dependencies>

        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>9.4.0.jre11</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>Logging</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>
<!--    String Driver= "com.microsoft.sqlserver.jdbc.SQLServerDriver";-->
<!--    String dbusername="sa";-->
<!--    String dbpassword="Admin@123";-->
<!--    String connectionString="jdbc:sqlserver://localhost:1433;databasename=Users;";-->
<!--    -->


    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

</project>

【问题讨论】:

    标签: java servlets glassfish


    【解决方案1】:

    这里我做了什么来解决上述问题。

    首先,我将我的第一个基本 java 项目转换为 maven 项目。 其次,我为第二个项目(web 项目)添加了 maven 依赖项,还使用 ​​InteliJ 中的以下选项在 web 项目的 web INF/lib 目录中添加了 jar 文件(第一个项目类)。 Image

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案2】:

    服务器的类加载器没有 LogFileCreator.class 的副本,因此该类不在其类路径中。

    【讨论】:

      【解决方案3】:

      这可能是部署文件(假设是战争)的打包方式。你是如何构建文件的? Maven、Gradle 都不是,等等?

      编辑

      我想为可能偶然发现类似问题的其他人提供更清晰的说明。在编译期间,有问题的项目/模块被导入;但是,maven 不知道将该库打包到生成的战争中。部署到 Glassfish 时,war 中不包含 LogFileCreator 类,因为它没有打包在 war 中。作者的解决方案奏效了,因为 maven 正在将模块打包到战争中,而 Glassfish 现在可以找到该类。

      【讨论】:

      • 感谢您的回复。我在 post 中附加了 pom.xml 。请检查并建议是否有任何问题。
      猜你喜欢
      • 2011-11-30
      • 2016-02-08
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 2020-06-17
      • 2012-02-27
      相关资源
      最近更新 更多