Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

a crazy java problem

49 views
Skip to first unread message

mche...@gmail.com

unread,
Nov 10, 2017, 9:41:45 PM11/10/17
to
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

mche...@gmail.com

unread,
Nov 10, 2017, 9:43:46 PM11/10/17
to
mche...@gmail.com於 2017年11月11日星期六 UTC+8上午10時41分45秒寫道:
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();
}

Arne Vajhøj

unread,
Nov 10, 2017, 10:53:42 PM11/10/17
to
On 11/10/2017 9:43 PM, mche...@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
0 new messages