Как запустить файл jar на javascript с аргументами?
Я создаю дискорд бота для запуска jar файла. Вот код:
const Discord = require('discord.js'); const { spawn } = require('child_process'); const client = new Discord.Client();
const token = ''; const runJavaCommand = '!runjava';
client.on('ready', () => { console.log('Bot is ready'); });
client.on('message', (msg) => { // Игнорируйте сообщения от ботов и неполные команды if (msg.author.bot || !msg.content.startsWith(runJavaCommand)) { return; }
const args = msg.content.split(' ');
const javaArgs = args.slice(1); // Получите аргументы после команды
// Запуск Java-приложения с аргументами
const javaProcess = spawn('java', ['-jar', 'your_java_app.jar', ...javaArgs]);
javaProcess.stdout.on('data', (data) => {
// Отправьте вывод вашего Java-приложения в канал Discord
msg.channel.send(`\`\`\`${data.toString()}\`\`\``);
});
javaProcess.stderr.on('data', (data) => {
// Отправьте ошибки вашего Java-приложения в канал Discord
msg.channel.send(`Error: \`\`\`${data.toString()}\`\`\``);
});
javaProcess.on('close', (code) => {
// Сообщите о завершении работы Java-приложения
msg.channel.send(`Java process exited with code ${code}`);
});
});
client.login('');
Код успешно запускает бота, но когда я пишу команду в консоли, происходит это:
[Pterodactyl Daemon]: Checking server disk space usage, this could take a few seconds... [Pterodactyl Daemon]: Updating process configuration files... [Pterodactyl Daemon]: Ensuring file permissions are set correctly, this could take a few seconds... container@pterodactyl~ Server marked as starting... [Pterodactyl Daemon]: Pulling Docker container image, this could take a few minutes to complete... [Pterodactyl Daemon]: Finished pulling Docker container image v18.15.0 :/home/container$ if [[ -d .git ]] && [[ ${AUTO_UPDATE} == "1" ]]; then git pull; fi; if [[ ! -z ${NODE_PACKAGES} ]]; then /usr/local/bin/npm install ${NODE_PACKAGES}; fi; if [[ ! -z ${UNNODE_PACKAGES} ]]; then /usr/local/bin/npm uninstall ${UNNODE_PACKAGES}; fi; if [ -f /home/container/package.json ]; then /usr/local/bin/npm install; fi; /usr/local/bin/node /home/container/${JS_FILE}
up to date, audited 149 packages in 819ms 17 packages are looking for funding run npm fund for details found 0 vulnerabilities Bot is ready node:events:491 throw er; // Unhandled 'error' event ^ Error: spawn java ENOENT at ChildProcess._handle.onexit (node:internal/child_process:283:19) at onErrorNT (node:internal/child_process:476:16) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) Emitted 'error' event on ChildProcess instance at: at ChildProcess._handle.onexit (node:internal/child_process:289:12) at onErrorNT (node:internal/child_process:476:16) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { errno: -2, code: 'ENOENT', syscall: 'spawn java', path: 'java', spawnargs: [ '-jar', 'your_java_app.jar' ] } Node.js v18.15.0 container@pterodactyl~ Server marked as offline... [Pterodactyl Daemon]: ---------- Detected server process in a crashed state! ---------- [Pterodactyl Daemon]: Exit code: 1 [Pterodactyl Daemon]: Out of memory: false [Pterodactyl Daemon]: Checking server disk space usage, this could take a few seconds... [Pterodactyl Daemon]: Updating process configuration files... [Pterodactyl Daemon]: Ensuring file permissions are set correctly, this could take a few seconds... container@pterodactyl~ Server marked as starting... [Pterodactyl Daemon]: Pulling Docker container image, this could take a few minutes to complete... [Pterodactyl Daemon]: Finished pulling Docker container image v18.15.0 :/home/container$ if [[ -d .git ]] && [[ ${AUTO_UPDATE} == "1" ]]; then git pull; fi; if [[ ! -z ${NODE_PACKAGES} ]]; then /usr/local/bin/npm install ${NODE_PACKAGES}; fi; if [[ ! -z ${UNNODE_PACKAGES} ]]; then /usr/local/bin/npm uninstall ${UNNODE_PACKAGES}; fi; if [ -f /home/container/package.json ]; then /usr/local/bin/npm install; fi; /usr/local/bin/node /home/container/${JS_FILE}
up to date, audited 149 packages in 736ms 17 packages are looking for funding run npm fund for details found 0 vulnerabilitiesk Bot is ready