繁体   English   中英

如何使用 intelijj 在 java web 项目中将 java 项目作为模块导入?

[英]How to import java project as module in java web project using intelijj?

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

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

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

下面的 class 来自 web 项目

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
    {

    }


}

下面的 class 来自正常的 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();
    }
}

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

早些时候我在同一个 web 项目中使用 LogFileCreator.java class 并且工作正常

Here what i am trying to acheive is,without writing the LogFileCreator class again in web project,wants to reuse the class written already inside normal java project to print the logs in desired text file.

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

[编辑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>

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

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

首先,我将我的第一个基本 java 项目转换为 maven 项目。 secondly I added maven dependency for the 2nd project (web project) and also added the jar file (of first project classes ) inside the web INF/lib directory of web project using below option from InteliJ. 图片

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

暂无
暂无

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

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