第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 NodeJS 運行 Java 函數(shù)

通過 NodeJS 運行 Java 函數(shù)

慕蓋茨4494581 2022-06-09 11:20:15
我有一個表單數(shù)據(jù)向我的 Express 服務(wù)器發(fā)送字符串和整數(shù),我需要使用我的 Java 后端進行計算和對前端的響應(yīng),我可以直接從 Express 服務(wù)器執(zhí)行此操作,還是需要涉及其他步驟?
查看完整描述

2 回答

?
12345678_0001

TA貢獻1802條經(jīng)驗 獲得超5個贊

您可以使用 Node.js 'exec' 調(diào)用外部 Java 程序,如下所示:


Javascript程序


const exec = require('child_process').exec;


// Number 7 is a command line argument to pass to the Java program

exec('java MyJavaApplication 7', function callback(error, stdout, stderr){

    console.log(stdout);

});

Java程序


public class MyJavaApplication {

    public static void main(String[] args) {

        int input = Integer.parseInt(args[0]);

        int output = calculate(input);

        System.out.println(Integer.toString(output));

    }


    private static int calculate(int input) {

        // Do some complex calculation

        return input * input;

    }

}

在 Node.js 上,您可以捕獲 Java 程序?qū)懭霕藴瘦敵龅娜魏蝺?nèi)容。根據(jù)輸入的復(fù)雜程度,您可能希望將文件名作為參數(shù)傳遞給 Java 程序。例如,該文件可以具有 JSON 格式的輸入內(nèi)容。


查看完整回答
反對 回復(fù) 2022-06-09
?
幕布斯6054654

TA貢獻1876條經(jīng)驗 獲得超7個贊

您可以從 nodejs 執(zhí)行 java 命令。您可以通過 expressjs 路由器運行 exec 命令。對于最好的情況,我會使用 Java 創(chuàng)建另一個 API 并向該端點(微服務(wù))發(fā)出請求。但是如果你不想這樣做,你可以試試這個代碼示例;


const express = require('express')

const app = express()

const port = 3000

const exec = require('child_process').exec

app.get('/', (req, res) => {

  const child = exec('/usr/bin/java ~/example.jar', => (error, stdout, stderr) {

    if (err) {

        console.error(err);

        res.json({error: err, status: 500, errorOutput: stderr})

        return

    }

    // it is important to have json structure in your output or you need to create a logic which parse the output

    res.json(stdout)

  })

})


app.listen(port, () => console.log(`Example app listening on port ${port}!`)) 


查看完整回答
反對 回復(fù) 2022-06-09
  • 2 回答
  • 0 關(guān)注
  • 534 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號