Run java project with node-jre module

0

I am developing a node application from which I need to execute java code.

I am using the module: node-jre ( link ).

The example that comes on the page (Hello.class) works well with the code that comes on the page (I include it here) but compiled the 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 comes when I try to execute a project that I created in eclipse. The documentation says that in the directory 'java' it looks in the jar files, so I tried to export the project as .jar, but it does not work, it seems that it does not find the main class that is what happened to it.

So, that's the question, how can I do to run my java project with node-jre?

I think it's important to say that the project has external libraries and also a single package with 7 classes of which only one is the main one (it has the main method).

    
asked by aitana 14.07.2017 в 11:27
source

2 answers

0

Solution:

var output = jre.spawnSync(  // call synchronously 
        ['java/hello.jar'],                // add the relative directory 'java' to the class-path 
        'com.package.example.Hello',                 // call main routine in class 'Hello' 
        ['World'],               // pass 'World' as only parameter 
        { encoding: 'utf8' }     // encode output as string 
      ).stdout.trim(); 
  • Complete path to the jar file
  • The name of the main class with all packages (separating with.)
answered by 17.07.2017 / 09:10
source
0

According to the documentation of the module you are occupying

 classpath (array of strings). Paths to .jar files or directories containing .classfiles.

You have to put your Java classes that you are going to use (.class) and the .jar of the external libraries (erroneously called libraries in Spanish) that you are going to occupy. Remember that the classpath is the way Java knows where to take the jar for execution.

About the package, remember that you only have to add the .class that are the Java compiled classes. If you are going to leave the package structure (which are actually folders) then imagine you should call them based on the package name. That is, if your Main Class is in

com/ferroblesh/Main.class

You should call it by its name with the package:

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