0

I have a maven project where i'm trying to read a javascript file but it can't find it and get a NullPointerException. This is a followup question on THIS POST

I have the method connect() in the class Process:

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));
  }
}

Is "/Script.js" the problem? and what should it be?

Project file Structure:

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

Inside the jar:

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

Target Folder:

When maven creates a jar file it generates a target folder, Not sure if this add any additional information to the question?

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

EDIT:

I get this:

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

when:

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

EDIT 2:

Without the /

("Script.js")

I get:

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)

EDIT 3 Maven dependency

    <!-- 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>
noo
  • 304
  • 1
  • 9
  • Have you tried to remove Slash before js file? – Stan Apr 11 '22 at 14:25
  • Try with `InputStream in = Process.class.getClassLoader().getResourceAsStream("Script.js")` – Matteo NNZ Apr 11 '22 at 14:28
  • @StanPeng Yep, I get the additional errors: `at java.base/java.io.Reader.(Reader.java:167)` `at java.base/java.io.InputStreamReader.(InputStreamReader.java:109` as wall ast the NPE – noo Apr 11 '22 at 14:30
  • @MatteoNNZ I still get `Exception in thread "main" java.lang.NullPointerException` – noo Apr 11 '22 at 14:35
  • Are you getting the error when running the program from your IDE? Or when running the Jar? Using "java -jar project-1.0-SNAPSHOT.jar"? What is the classpath for the execution? – Zephyr Apr 11 '22 at 14:47
  • @Zephyr "java -jar project-1.0-SNAPSHOT.jar", I'm using VS Code as an IDE but using the terminal. path is project/target – noo Apr 11 '22 at 14:55
  • Can you post the whole stack trace in the question? – Matteo NNZ Apr 11 '22 at 15:33
  • Javascript and Java are completely different languages. In the future, tag only the relevant one. This time, I have edited your tags for you. – John Bollinger Apr 11 '22 at 15:46
  • Yeah, I don't see the Graaljs Scriptengine artifacts anywhere there. They'll also need to be included on the classpath (for example, in the form of a shaded jar; or listed in the Jar's Manifest.mf's Class-Path). – Zephyr Apr 11 '22 at 16:08
  • @user16320675 That could be a possibility, How would I check that? Sorry i'm new to using maven. I made a edit for the dependency. – noo Apr 11 '22 at 16:18
  • https://stackoverflow.com/a/1729094/4798347 shows an example of how to include dependencies with your Jar. You'll know that it worked if you can see the dependency packages / classes / resources in your built project-1.0-SNAPSHOT.jar – Zephyr Apr 12 '22 at 13:26
  • @Zephyr thank you This helped! I've got it working now. I did't have the dependencies in the jar file :) – noo Apr 12 '22 at 19:50

2 Answers2

1

Pom.xml Structure

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

this will produce 2 jar files, one with dependencies and without.

0

Including dependencies in a 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>
noo
  • 304
  • 1
  • 9