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