简体   繁体   English

Java-Arduino-使用IDE(禁用了GUI)上传草图时出错,但可从命令行运行

[英]Java - Arduino - Error uploading sketch using IDE (with GUI disabled), but it works from command line

I'm trying to run this command: 我正在尝试运行以下命令:

"C:\arduino\arduino --upload --board arduino:avr:nano --port COM3 -v ..\config\config.ino"

It works from my command line. 它可以从我的命令行运行。 It takes just a few seconds to run & finish. 只需几秒钟即可运行并完成。

The problem comes when I try to execute it from Java with: 当我尝试使用以下命令从Java执行它时,问题就来了:

String cmd = "C:\arduino\arduino --upload --board arduino:avr:nano --port COM3 -v ..\config\config.ino"
Process p = Runtime.getRuntime().exec(cmd);

It takes a minute to execute and ends with: 执行需要一分钟,以:

Launch 4j: an error occurred while starting the application 启动4j:启动应用程序时发生错误

I've tried with ProcessBuilder too. 我也尝试过使用ProcessBuilder。 I've also tried saving the command to a batch file and then running the batch file from Java...but I just got the same result. 我也尝试将命令保存到批处理文件中,然后从Java运行批处理文件...但是我得到的结果相同。

EDIT: 编辑:

If I run the batch file from command line it works too. 如果我从命令行运行批处理文件,它也可以工作。 As I said if I run it from Java, it doesn't. 就像我说的那样,如果我从Java运行它,则不会。

I've realized that if I run the batch file from another batch file it doesn't work neither. 我已经意识到,如果我从另一个批处理文件运行该批处理文件,则两者都不起作用。

Maybe there is not a problem at all with Java, but with Arduino IDE. 也许Java根本没有问题,但是Arduino IDE完全没有问题。

EDIT 2: Adding "start" parameter before the command and saving it to a batch file seems to work. 编辑2:在命令之前添加“开始”参数并将其保存到批处理文件似乎工作。 Then you just run the batch from java. 然后,您只需从Java运行批处理。

Something like this: 像这样:

arduino.bat arduino.bat

"start C:\arduino\arduino --upload --board arduino:avr:nano --port COM3 -v ..\config\config.ino"

Java code Java代码

String s = "PATH TO ARDUINO.BAT"
Process p = null;
ProcessBuilder pb = new ProcessBuilder(s + "arduino.bat");
pb.directory(new File(s));
p = pb.start();

I think the issue is with the relative path at -v argument. 我认为问题出在-v参数的相对路径上。 Use full path or set actual working directory with ProcessBuilder . 使用完整路径或通过ProcessBuilder设置实际工作目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM