Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda