Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.NoClassDefFoundError after compiled into executable #566

Open
Y-christian opened this issue Jan 5, 2023 · 1 comment
Open

Comments

@Y-christian
Copy link

i tried to make a small node js calling jar file that function to call hello world it works normall if i just run node test.js. but if i build the package into .exe using pkg method it gives out this error can someone help me?

var clazz = java.findClassSync(name); // TODO: change to Class.forName when classloader issue is resolved.
                   ^

Error: Could not create class Test
java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)

    at Java.import (C:\snapshot\test-node-java\node_modules\←[4mjava←[24m\lib\nodeJavaBridge.js:227:20)
    at Object.<anonymous> (C:\snapshot\test-node-java\test.js:4:23)
    at Module._compile (pkg/prelude/bootstrap.js:1930:22)
←[90m    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)←[39m
←[90m    at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
    at Function.runMain (pkg/prelude/bootstrap.js:1983:12)
←[90m    at node:internal/main/run_main_module:17:47←[39m {
  cause: nodeJava_java_lang_NoClassDefFoundError {}
}

this is the js code

var java = require('java');

java.classpath.push('test-node-java.jar');
let test = java.import('Test');
test.hello();
console.log(test)

this is the java code

import java.lang.System;

public class Test {
    public static void hello() {
        System.out.println("Hello Java");
    }
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

this is the package.json

{
  "name": "test-node-java",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "bin": "test.js",
  "scripts": {
    "start": "node test.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "pkg . --target node16-win-x64"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "java": "^0.12.2",
    "java-caller": "^2.7.0"
  },
  "devDependencies": {
    "pkg": "^5.8.0"
  },
  "pkg": {
    "outputPath": ".dist",
    "assets": [
      "./node_modules/**/*"
    ]
  }
}

all the files are under the same directory including the test-node-java.jar

@Y-christian Y-christian changed the title java.lang.NoClassDefFoundErrorafter complie into executable java.lang.NoClassDefFoundError after compiled into executable Jan 5, 2023
@velara3
Copy link

velara3 commented May 1, 2024

Did you get this working?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants