1

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

2

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

5 Comments

i am a little stupid so can i ask will the path be home/container/src or just /src cuz in hostings we get a container in their home directory so our path will be relative to our container out the hosting's home dir?
I would recommend using relative path the tild stands for your home directory so you can do ~/container/src
The benefit is that if you move your project to a different unix system it will still work and you don't have to update your path
Alex it wowrked but i needed to change something writing this to let you know exec("cd ~/ && java -jar Lavalink.jar ", (error, stdout, stderr) => { this cded into my container if i added cd ~/container it went to home/container/container
@EliteGaming what did you change, the path to ~/ ?If you think this was helpful you can mark it as accepted the sign below the up-/downvote sign.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.