gpt4 book ai didi

java - 如何在运行时使用java代码执行脚本

转载 作者:太空宇宙 更新时间:2023-11-04 08:35:01 24 4
gpt4 key购买 nike

我有一个Java 应用程序和一个NSIS 脚本。我需要在运行时通过 Java 代码运行 NSIS 脚本。我知道以下代码用于运行时执行。

Process p = Runtime.getRuntime().exec();

我的问题是我不知道如何在exec()中执行我的脚本。如果有人请告诉我如何在我的 exe 中使用这个脚本。

我的 NSIS 脚本是:

OutFile "Your ComputerName.exe"
Name "Your ComputerName"
Caption "ComputerName"
XPStyle "on"

Function .onInit
ReadRegStr $0 HKLM "System\CurrentControlSet\Control\ComputerName\ActiveComputerName" "ComputerName"
StrCmp $0 "" win9x
StrCpy $1 $0 4 3
MessageBox MB_OK "Your ComputerName : $0"
Goto done
win9x:
ReadRegStr $0 HKLM "System\CurrentControlSet\Control\ComputerName\ComputerName" "ComputerName"
StrCpy $1 $0 4 3
MessageBox MB_OK "Your ComputerName : $0"
done:
Quit ; placed here so we quit the installer; we dont need the other pages for this example.
FunctionEnd

Section "-boo"
;
SectionEnd

; rest of script

提前致谢..

嗨,我使用以下 java 代码:

import java.io.IOException;


public class SampleClass {

/**
* @param args
*/
Process p;
public static void main(String[] args) {
// TODO Auto-generated method stub
Runtime r=Runtime.getRuntime();
try {
r.exec("makensis.exe myscript.nsi");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

我的 NSIS 脚本与上面相同,当我执行时,我收到以下错误,\

java.io.IOException: Cannot run program "makensis.exe": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at SampleClass.main(SampleClass.java:14)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more

为什么会出现这个错误如何解决???

最佳答案

我不熟悉 NSIS 脚本,但快速谷歌搜索告诉我,我可以在命令提示符下运行 NSIS 脚本:makensis.exe myscript.nsi

所以在java中你可以这样做:

 Process p = Runtime.getRuntime().exec("makensis.exe myscript.nsi")

然后你可以使用p来获取执行结果的stdout和stderr文本。为此,您必须确保 makensis.exe 位于您的 PATH 环境变量中的目录下。

关于java - 如何在运行时使用java代码执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6609679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com