NullPointerException - 查找资源文件

NullPointerException - find resource file

我有一个 Maven 项目,我正在尝试读取一个 javascript 文件,但找不到它并得到一个 NullPointerException。这是关于

的后续问题

我在 class 进程中有 connect() 方法:

public void connect() throws IOException{
  ScriptEngineManager manager = new ScriptEngineManager();
  ScriptEngine graalEngine = manager.getEngineByName("graal.js");

  try (InputStream in = Process.class.getResourceAsStream("/Script.js")) {
    graalEngine.eval(new InputStreamReader(in, StandardCharsets.UTF_8));
  }
}

"/Script.js" 是问题吗?它应该是什么?

项目文件结构:

src
 ┣ main
 ┃ ┣ java
 ┃ ┃ ┗ com
 ┃ ┃ ┃ ┗ group
 ┃ ┃ ┃ ┃ ┣ App.java
 ┃ ┃ ┃ ┃ ┗ Processor.java
 ┃ ┗ resources
 ┃ ┃ ┗ Script.js

罐子里面:

 project-1.0-SNAPSHOT
 ┃ ┣ META-INF
 ┃ ┃ ┣ maven
 ┃ ┃ ┃ ┗ com.group
 ┃ ┃ ┃ ┃ ┗ project
 ┃ ┃ ┃ ┃ ┃ ┣ pom.properties
 ┃ ┃ ┃ ┃ ┃ ┗ pom.xml
 ┃ ┃ ┗ MANIFEST.MF
 ┃ ┣ com
 ┃ ┃ ┗ group
 ┃ ┃ ┃ ┣ App.class
 ┃ ┃ ┃ ┗ Process.class
 ┃ ┣ Script.js

目标文件夹:

当 maven 创建一个 jar 文件时,它会生成一个目标文件夹,不确定这是否为问题添加了任何附加信息?

target
 ┣ classes
 ┃ ┣ com
 ┃ ┃ ┗ group
 ┃ ┃ ┃ ┣ App.class
 ┃ ┃ ┃ ┗ Process.class
 ┃ ┣ Script.js
 ┣ generated-sources
 ┃ ┗ annotations
 ┣ generated-test-sources
 ┃ ┗ test-annotations
 ┣ maven-archiver
 ┃ ┗ pom.properties
 ┣ maven-status
 ┃ ┗ maven-compiler-plugin
 ┃ ┃ ┣ compile
 ┃ ┃ ┃ ┗ default-compile
 ┃ ┃ ┃ ┃ ┣ createdFiles.lst
 ┃ ┃ ┃ ┃ ┗ inputFiles.lst
 ┃ ┃ ┗ testCompile
 ┃ ┃ ┃ ┗ default-testCompile
 ┃ ┃ ┃ ┃ ┣ createdFiles.lst
 ┃ ┃ ┃ ┃ ┗ inputFiles.lst
 ┣ surefire-reports
 ┃ ┣ TEST-com.group.AppTest.xml
 ┃ ┗ com.group.AppTest.txt
 ┣ test-classes
 ┃ ┗ com
 ┃ ┃ ┗ group
 ┃ ┃ ┃ ┗ AppTest.class
 ┗ project-1.0-SNAPSHOT.jar

编辑:

我明白了:

Exception in thread "main" java.lang.NullPointerException
    at com.group.Process.connect(Process.java:109)
    at com.group.App.main(App.java:47)

时间:

    try (InputStream in = Process.class.getClassLoader().getResourceAsStream("/Script.js")) {
       graalEngine.eval(new InputStreamReader(in, StandardCharsets.UTF_8));
    }

编辑 2:

没有/

("Script.js")

我得到:

Exception in thread "main" java.lang.NullPointerException
    at java.base/java.io.Reader.<init>(Reader.java:167)
    at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:109)
    at com.group.Process.connect(Process.java:109)
    at com.group.App.main(App.java:47)

编辑 3 Maven 依赖项

    <!-- https://mvnrepository.com/artifact/org.graalvm.js/js-scriptengine -->
    <dependency>
      <groupId>org.graalvm.js</groupId>
      <artifactId>js-scriptengine</artifactId>
      <version>22.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.graalvm.js/js -->
    <dependency>
      <groupId>org.graalvm.js</groupId>
      <artifactId>js</artifactId>
      <version>22.0.0.2</version>
    </dependency>

在 jar 中包含依赖项

Including dependencies in a jar with Maven

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <mainClass>com.group.App</mainClass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
              <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

Pom.xml结构

<project>
    <!--Information-->
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!--PROJECT DEPENDENCIES-->
    <dependencies>
      <!--List all Dependencies-->
      <dependency>
        ...
      </dependency>
      ...
    </dependency>

    <!-- Build JAR-->
    <build>
      <finalName>name_of_jar</finalName>
    <plugins>
    <!--Build JAR With Dependencies-->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <mainClass>directory_of_file_with_main</mainClass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>name_of_jar_with_dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
          <executions>
            <execution>
              <id>make-assembly</id>
              <phase>package</phase>
              <goals>
              <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

        <!--LIFECYCLES-->
        <plugin>
          ...
        </plugin>
        ...
    </plugins>
  </build>
</project>

这将生成 2 个 jar 文件,一个有依赖项,一个没有。