Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37648 > unrolled thread
| Started by | mcheung63@gmail.com |
|---|---|
| First post | 2017-11-10 18:41 -0800 |
| Last post | 2017-11-10 22:53 -0500 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
a crazy java problem mcheung63@gmail.com - 2017-11-10 18:41 -0800
Re: a crazy java problem mcheung63@gmail.com - 2017-11-10 18:43 -0800
Re: a crazy java problem Arne Vajhøj <arne@vajhoej.dk> - 2017-11-10 22:53 -0500
| From | mcheung63@gmail.com |
|---|---|
| Date | 2017-11-10 18:41 -0800 |
| Subject | a crazy java problem |
| Message-ID | <33ab661b-810a-4d40-86ef-907752ec35aa@googlegroups.com> |
Hi All
this code can run successfull in a simple java main():
try {
System.out.println(System.getProperty("java.version"));
String str = CommonLib.runCommand("C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe -Tpng C:\\Users\\Peter\\AppData\\Local\\Temp\\1.asm6434829820851445658.dot -o C:\\Users\\Peter\\desktop\\1.asm3568795673124410656.png");
System.out.println("str=" + str);
} catch (Exception ex) {
ex.printStackTrace();
}
But if running inside the netbeans plugin module, it throws an exception:
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>(ProcessImpl.java:386)
at java.lang.ProcessImpl.start(ProcessImpl.java:137)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
Caused: java.io.IOException: Cannot run program "C:\Program": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at com.peterswing.CommonLib.runCommand(CommonLib.java:730)
at com.peterswing.CommonLib.runCommand(CommonLib.java:721)
[catch] at com.github.mcheung63.netbeans.antlr.FileTypeG4VisualElement.jButton1ActionPerformed(FileTypeG4VisualElement.java:249)
at com.github.mcheung63.netbeans.antlr.FileTypeG4VisualElement.access$500(FileTypeG4VisualElement.java:44)
at com.github.mcheung63.netbeans.antlr.FileTypeG4VisualElement$6.actionPerformed(FileTypeG4VisualElement.java:144)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
Any hints?
Thanks
From Peter
[toc] | [next] | [standalone]
| From | mcheung63@gmail.com |
|---|---|
| Date | 2017-11-10 18:43 -0800 |
| Message-ID | <37cbaf57-b52e-4f12-93c1-e3da6b01c9d9@googlegroups.com> |
| In reply to | #37648 |
mche...@gmail.com於 2017年11月11日星期六 UTC+8上午10時41分45秒寫道:
> Hi All
> this code can run successfull in a simple java main():
>
> try {
> System.out.println(System.getProperty("java.version"));
> String str = CommonLib.runCommand("C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe -Tpng C:\\Users\\Peter\\AppData\\Local\\Temp\\1.asm6434829820851445658.dot -o C:\\Users\\Peter\\desktop\\1.asm3568795673124410656.png");
> System.out.println("str=" + str);
> } catch (Exception ex) {
> ex.printStackTrace();
> }
>
>
> But if running inside the netbeans plugin module, it throws an exception:
>
> 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>(ProcessImpl.java:386)
> at java.lang.ProcessImpl.start(ProcessImpl.java:137)
> at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
> Caused: java.io.IOException: Cannot run program "C:\Program": CreateProcess error=2, The system cannot find the file specified
> at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
> at java.lang.Runtime.exec(Runtime.java:620)
> at java.lang.Runtime.exec(Runtime.java:450)
> at java.lang.Runtime.exec(Runtime.java:347)
> at com.peterswing.CommonLib.runCommand(CommonLib.java:730)
> at com.peterswing.CommonLib.runCommand(CommonLib.java:721)
> [catch] at com.github.mcheung63.netbeans.antlr.FileTypeG4VisualElement.jButton1ActionPerformed(FileTypeG4VisualElement.java:249)
> at com.github.mcheung63.netbeans.antlr.FileTypeG4VisualElement.access$500(FileTypeG4VisualElement.java:44)
> at com.github.mcheung63.netbeans.antlr.FileTypeG4VisualElement$6.actionPerformed(FileTypeG4VisualElement.java:144)
> at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
> at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
> at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
> at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
> at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
> at java.awt.Component.processMouseEvent(Component.java:6533)
> at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
> at java.awt.Component.processEvent(Component.java:6298)
>
> Any hints?
> Thanks
> From Peter
public static String runCommand(String command, int skipLine) throws Exception {
StringBuffer sb = new StringBuffer(4096);
int x = 0;
String s;
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
if (x >= skipLine) {
sb.append(s);
sb.append(System.getProperty("line.separator"));
}
x++;
}
stdInput.close();
return sb.toString();
}
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2017-11-10 22:53 -0500 |
| Message-ID | <ou5s7h$97m$1@gioia.aioe.org> |
| In reply to | #37649 |
On 11/10/2017 9:43 PM, mcheung63@gmail.com wrote:
> mche...@gmail.com於 2017年11月11日星期六 UTC+8上午10時41分45秒寫道:
>> this code can run successfull in a simple java main():
>> String str = CommonLib.runCommand("C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe -Tpng C:\\Users\\Peter\\AppData\\Local\\Temp\\1.asm6434829820851445658.dot -o C:\\Users\\Peter\\desktop\\1.asm3568795673124410656.png");
>> But if running inside the netbeans plugin module, it throws an exception:
>>
>> java.io.IOException: CreateProcess error=2, The system cannot find the file specified
>> Caused: java.io.IOException: Cannot run program "C:\Program": CreateProcess error=2, The system cannot find the file specified
>> Any hints?
> public static String runCommand(String command, int skipLine) throws Exception {
> Process p = Runtime.getRuntime().exec(command);
I am more puzzled over why it sometimes work.
As I read the JavaDoc for Runtime.exec overloads then command line
get parsed to parts simply by splitting at spaces. That does not
find the correct command.
So I will suggest:
String str = CommonLib.runCommand(new String[] { "C:\\Program Files
(x86)\\Graphviz2.38\\bin\\dot.exe", "-Tpng",
"C:\\Users\\Peter\\AppData\\Local\\Temp\\1.asm6434829820851445658.dot",
"-o", "C:\\Users\\Peter\\desktop\\1.asm3568795673124410656.png" });
public static String runCommand(String[] cmdarr, int skipLine) throws
Exception {
Process p = Runtime.getRuntime().exec(cmdarr);
Arne
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web