question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

node-java java.import not working with a packaged electron app

See original GitHub issue

Cross reporting the issue reported in node-java https://github.com/joeferner/node-java/issues/352

With a package app using an asar archive, java.import does not work. The following line of code will throw an error:

var DateTime = java.import('org.joda.time.DateTime');

Error: Could not create class org.joda.time.DateTime
java.lang.NoClassDefFoundError: org/joda/time/DateTime
Caused by: java.lang.ClassNotFoundException: org.joda.time.DateTime
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

    at Error (native)
    at Java.java.import (...)

Only the packaged version of the app fails. Launching the unpackaged electron app will work fine.

The app was packaged with the following command:

electron-packager ./app --out=dist --asar.unpack='**/node_modules/java/**/*'

I guess the function java.import needs to be adapted in order to work with packaged electron apps.

Let me know if I can provide more information.

regards

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kevinsawickicommented, Sep 19, 2016

@antoinepairet I think the issue is that the internal classpath paths in node-java need to be using the asar.unpacked directory instead of the app.asar one. https://github.com/joeferner/node-java/blob/126bfa821cefcb8af21e323990d9985936938a97/lib/nodeJavaBridge.js#L23-L31

I got it working with the following patch to your repo:

diff --git a/renderer.js b/renderer.js
index c2997dc..9744f4b 100644
--- a/renderer.js
+++ b/renderer.js
@@ -3,10 +3,18 @@
 // All of the Node.js APIs are available in this process.

 'use strict';
-const java = require('java');
+
+const path = require('path');
+
+let java
+if (path.extname(__dirname) === '.asar') {
+  java = require('../app.asar.unpacked/node_modules/java');
+} else {
+  java = require('java');
+}

 var list1 = java.newInstanceSync("java.util.ArrayList");
 console.log(list1.sizeSync()); // 0
 list1.addSync('item1');

-module.exports = list1.sizeSync();
\ No newline at end of file
+module.exports = list1.sizeSync();
0reactions
gpinkhamcommented, May 4, 2019

@antoinepairet I know this is a really old issue but curious. how did that code work for you? building the electron app with webpack ends up including both requires (it doesn’t run the if statement). Did you use some other packer? thanks!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot install NPM package "java" alongside Electron
Before installing Java, we need to install Windows build tools using the following command: npm install --global --production windows-build- ...
Read more >
java-electron - npm
Start using java-electron in your project by running `npm i ... status 1" Try running node findJavaHome.js in the node-java directory to see ......
Read more >
Can Aspose.Cells for Javascript via Java run in Electron?
I realize this is an issue with the node-java package and not aspose, but figured I'd ask. Any help would be appreciated. Thanks!...
Read more >
java-bridge - NPM Package Overview - Socket - Socket.dev
node-java ... In contrast to other node.js <-> java interfaces, the binary is not hard ... When using this package in a packaged...
Read more >
File Upload in ElectronJS - GeeksforGeeks
For Electron to work, node and npm need to be pre-installed in the system. ... To import and use the dialog Module in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found