如何在运行时使用java代码执行脚本
我有一个 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
为什么会发生如何解决此错误??? ??
I have a Java application and a NSIS script. I need to run the NSIS script via Java code at runtime. I know the following code is used for runtime execution.
Process p = Runtime.getRuntime().exec();
My problem is i don't know how to execute my script with in exec(). if anyone please tells me how I use this script with in my exe.
My NSIS script is:
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
Thanks in Advance..
Hi i use the following java code:
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();
}
}
}
and my NSIS script is same as above, while i'm executing i got the following error,\
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
Why it happens how to resolve this error?????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉 NSIS 脚本,但是快速谷歌搜索告诉我我可以在命令提示符下运行 NSIS 脚本:
makensis.exe myscript.nsi
所以在 java 中你可以这样做:
然后你可以使用
p
获取执行结果的stdout和stderr文本。为此,您必须确保 makensis.exe 位于您的 PATH 环境变量中的目录下。I'm not familiar with NSIS script, but quick Googling showed me I can run the NSIS script like this in command prompt:
makensis.exe myscript.nsi
So in java you can do this:
then you can use
p
to get the stdout and stderr text of the execution result. For this to work though you have to make suremakensis.exe
is under the directory in yourPATH
environmental variable.这并不是您问题的真正答案,但您想要实现的目标也可以在 Java 中完成。
使用此代码以及附加的类:
您需要一个用于处理 Windows 注册表的棘手类:
WinRegistry.java
(取自 David 对 StackOverflow 问题的回答)
This isn't really an answer on your question, but what you want to achieve with that can be done in Java as well.
Use this code, together with the class attached:
You need a tricky class which is used to work with the Windows Registry:
WinRegistry.java
(Taken from David's answer on a StackOverflow Question)