Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
How can I run java file inside Node.js?

I just got the task to create a java file, that works as JavaScript under Node.js, and as Java code also. The file extension has to be .java. My question is - how can I even run the .java file in the node? I wasn't able to find anything about an issue like this. The code itself is very simple, so the only thing is - how to run .java in node?

I can't use npm pack. It must be vanilla code.

so

node test.java > output

javac test.java && java test > output

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

With the normal API, unless you use Wasmer and run it with WebAssembly, (which I doubt you want to do, especially considering you can't use npm packages), you can not do this with a java running library inside.

However, you can spawn a child process to run it:

const { exec } = require('child_process');

exec('javac Main.java', (err, stdout, stderr) => {
  if (err) {
    console.error(`exec error: ${err}`);
    return;
  }

  exec('java Main', (err, stdout, stderr) => {
    if (err) {
      console.error(`exec error: ${err}`);
      return;
    }
    
    console.log(`stdout: ${stdout}`);
    console.log(`stderr: ${stderr}`);
  })
});

Though, I highly advise to port the Java code you have to Node.JS.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!