Issue
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 href="https://stackoverflow.com/questions/71809998/access-a-file-from-a-resources-folder-in-java-run-a-javascript-file/71810173#71810173">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>
Solution
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.
Answered By - inspectorconfusion
Answer Checked By - Marie Seifert (JavaFixing Admin)