Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Hello I have got a jar file lavalink to be exact which is basically a music module now i would like to start the jar file from my index.js(Main bot file) as I am not in the financial state to pay for a separate host for the lavalink server and would like to start and use the lavalink server in the same hosting container

No I am not running it in a browser I am using discord.js wrapper to interact with the discord API to make a backend program

question from:https://stackoverflow.com/questions/65643700/how-to-start-a-java-file-from-a-javascript-file-node-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
84 views
Welcome To Ask or Share your Answers For Others

1 Answer

Installation

Install child process npm i child-process this allows you to execute shell comands

Command

give the exec function as first parameter your command

  • exec("cd ~/<path-to-directory-jarFile> && java -jar Myjar_file.jar

  • cd ~/<path-to-directory-jarFile> goes in the directory where your jar file is

  • java -jar Myjar_file.jar executes your jar file

  • the && executes your commands consecutevely first the one before then the one after

Example

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

exec("cd ~/<path-to-directory-jarFile>  && java -jar Myjar_file.jar ", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

Executing Shell Commands in NodeJS:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...