Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
How to use Exec / ExecFile on Javascript to run a java file and get the result

Running this JS file on the server side of my website so when the user presses a button, it executes this command with my API:

exports.runAlgorithm = async (req, res) => { 

    try {
        
        exec('java test.java', (error, stdout, stderr) => {
            if (error) {
                console.error(`${error}`);
                return;
            }


            try {
                //Export the result to the database
                Result.create({
                    //Should send the algorithm's result here
                    result: stdout
                })
                res.send({
                    message: "Algorithm executed successfully and output stored in database",
                });
            }
            catch (err) {
                res.send({
                    message: "Something went wrong when passing the data to the database",

                });
                console.log(`stderr: ${stderr}`);

            }

        });

    }
    catch (err) {
        res.send({
            message: "Problem with running the algo"
        });
    }
}

But I receive this error:

exec error: Error: Command failed: java test.java
Error: Could not find or load main class test.java
Caused by: java.lang.ClassNotFoundException: test.java

The API to run this code is the algorithm controller and the test file that just returns hello world is the test.java

Have also tried the execFile alternative but to no avail. Perhaps I'm just using it wrongly or there's a better way to do this.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Java runs compiled files, with ".class" extension.
You should compile you test.java class using javac.

javac test.java

Then, you should run java with the class name alone.

java test
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda