2

I am developing a node.js application where I need to execute java code.

I am using de node-jre module (https://www.npmjs.com/package/node-jre).

The example in this web (Hello.class) is running correctly with this code and compilling de Hello class by console with javac Hello.java:

    var output = jre.spawnSync(  // call synchronously 
    ['java'],                // add the relative directory 'java' to the class-path 
    'Hello',                 // call main routine in class 'Hello' 
    ['World'],               // pass 'World' as only parameter 
    { encoding: 'utf8' }     // encode output as string 
  ).stdout.trim(); 

The problem is when I tried to execute my java project (developed with eclipse). The documentation say that in this directory 'java' search inside jar files. So, it was the first thing I tried: to export the proyect to jar, but it does not work.

So, how can I execute a complete project (with only one main class)?, I think it is important to say that the project use external libraries.

The META-INF/MANIFEST.MF inside jar looks like:

Manifest-Version: 1.0
Class-Path: .
Main-Class: com.packagename.example1.ClassName

Name: org/apache/oro
Specification-Title: Jakarta ORO
Implementation-Title: org.apache.oro
Implementation-Version: 2.0.8 2003-12-28 11:00:13
Specification-Version: 2.0.8
Specification-Vendor: Apache Software Foundation
Implementation-Vendor: Apache Software Foundation

So, my code is:

 var output = jre.spawnSync(  // call synchronously 
    ['java'],                // add the relative directory 'java' to the class-path 
    'ClassName',                 // call main routine in class 'Hello' 
    ['World'],               // pass 'World' as only parameter 
    { encoding: 'utf8' }     // encode output as string 
  ).stdout.trim(); 

And I tried also with:

 var output = jre.spawnSync(  // call synchronously 
        ['java'],                // add the relative directory 'java' to the class-path 
        'com.packagename.example1.ClassName',                 // call main routine in class 'Hello' 
        ['World'],               // pass 'World' as only parameter 
        { encoding: 'utf8' }     // encode output as string 
      ).stdout.trim();

'java' was the folder that I have in node in the same level that server.js

3
  • What does your Manifest file look like inside the jar? Is the main class declared? The classpath? Commented Jul 14, 2017 at 11:32
  • In the Manifiest: Manifest-Version: 1.0 Class-Path: . Main-Class: com.package.example1.ClassName Commented Jul 14, 2017 at 11:42
  • You can edit your post if needs be to add details. Also please check how to create a MCVE: stackoverflow.com/help/mcve It will help anyone seeing the post to understand and reproduce your issue Commented Jul 14, 2017 at 11:45

1 Answer 1

1

The problem was that I just put the directory where the .jar file is and the correct way was to put the complete path to the jar file: java/Nameofjarfile.jar

 var output = jre.spawnSync(  // call synchronously 
        ['java/Nameofjarfile.jar'],                // add the relative directory 'java' to the class-path 
        'com.packagename.example1.ClassName',                 // call main routine in class 'Hello' 
        ['World'],               // pass 'World' as only parameter 
        { encoding: 'utf8' }     // encode output as string 
      ).stdout.trim();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.