uploading Code From java(eclipse)

hello,
i was wondering if i there is a way to upload arduino sketch from java;
i'm working on a school project where i need to create an .EXE for my program. so i need to include the uploading of arduino sketch by executing my executable file to make it all autonomous.

can someone give me and idea or a way to do that please???

Yes, the whole Arduino IDE is Java :wink:

But it ain't easy... But in a nutshell, the IDE uses AVRDude to upload the code. So if all you need is to upload a fixed sketch aka pre-compiled piece of code, just include AVRdude and use the appropriate command line commands :slight_smile:

You can call the Arduino IDE from the command line to get it to compile and upload a program. I use that with Python to allow me to upload programs from the Geany editor.

...R

Thank you for the idea; i 've always though about it but the aim is to upload without installing arduino IDE... i'm looking forward a good avrdude tutorial so i can runcommand inside my java app.

DrMessina:
but the aim is to upload without installing arduino IDE...

That is, of course, your privilege. But why are you asking about it on a Forum dedicated to the Arduino IDE ?

It's bit like going to the Ford dealer with a question about a Volkswagen.

And what is the objection to the Arduino IDE?

...R

DrMessina:
i was wondering if i there is a way to upload arduino sketch from java;
i'm working on a school project where i need to create an .EXE for my program. so i need to include the uploading of arduino sketch by executing my executable file to make it all autonomous.

An executable in Java is normally a 'jar' file not an 'exe' file. One can find 'exe' wrappers for 'jar' files and there are Java compilers that produce 'exe' directly but my experience of them is that they are very buggy. I use Eclipse with the AVR plugin for AVR development outside of Arduino. It works great though it can be annoying at times.

This all sounds to me like an XY problem .

Robin2:
It's bit like going to the Ford dealer with a question about a Volkswagen.

Mehh, I do go to a Ford dealer to ask about the Bosch alternator in my transit... :wink:

Robin2:
That is, of course, your privilege. But why are you asking about it on a Forum dedicated to the Arduino IDE ?

It's bit like going to the Ford dealer with a question about a Volkswagen.

And what is the objection to the Arduino IDE?

...R

this is related to the upload process of arduino. i just can't use the IDE because of the restriction of my project.
and i read the doc about uploading code from java and the doc about avrdude i understand the command but it doesn"t work that's why i'm asking for any other way to do it...

DrMessina:
i just can't use the IDE because of the restriction of my project.

If you explain that maybe we can help?

You can always read the Atmel datasheet for the microprocessor you are using to find out the basics of uploading code. There is nothing secret or magical about it.

...R

DrMessina:
this is related to the upload process of arduino. i just can't use the IDE because of the restriction of my project.
and i read the doc about uploading code from java and the doc about avrdude i understand the command but it doesn"t work that's why i'm asking for any other way to do it...

'avrdude' does work! I use it all the time. The Arduino IDE uses it. AVR Eclipse uses it. The Arduino Netbeans module uses it. What makes you think it does not work?

What 'restrictions' apply? Note - if you upload using Java the chances are you will need to spawn the GNU AVR tools and avrdude. Now this is exactly what the Arduino IDE does and AVR Eclipse does and Arduino Netbeans does.

I have used Makefiles to build and upload Arduino/AVR code but once again the GNU AVR tools are used together with 'avrdude' .

hello i'm uploading code from java to arduino using the same command arduino use can some one help me figure why this doesn't work? and why i get this error??
i have ever change the com port 3 times it doesnt change anything.

this is the command that i use:
String ArduinoPath = "bin/avrdude";
String confPath ="avrdude.conf";
String hexPath ="C:/Users/AMATAL~1/AppData/Local/Temp/arduino_build_669907/test.ino.hex";
String port = "COM1";
runCommand(new String[]{ArduinoPath, "-C"+confPath,"-v","-pATmega2560","-cwiring","-P"+port,"-b115200","-D","-Uflash:w:"+hexPath+":i"});

this is the error i get:

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "avrdude.conf"

Using Port : COM1
Using Programmer : wiring
Overriding Baud Rate : 115200
avrdude: ser_open(): can't open device "\.\COM1": The system cannot find the file specified.

avrdude: ser_drain(): read error: The handle is invalid.

avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
avrdude: stk500_send(): failed to send command to serial port
avrdude: ser_recv(): read error: The handle is invalid.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

avrdude done. Thank you.

i use

stowite:
'avrdude' does work! I use it all the time. The Arduino IDE uses it. AVR Eclipse uses it. The Arduino Netbeans module uses it. What makes you think it does not work?

What 'restrictions' apply? Note - if you upload using Java the chances are you will need to spawn the GNU AVR tools and avrdude. Now this is exactly what the Arduino IDE does and AVR Eclipse does and Arduino Netbeans does.

I have used Makefiles to build and upload Arduino/AVR code but once again the GNU AVR tools are used together with 'avrdude' .

i use this command:
String ArduinoPath = "bin/avrdude";
String confPath ="avrdude.conf";
String hexPath = "C:/Users/AMATAL~1/AppData/Local/Temp/arduino_build_669907/test.ino.hex";
String port = "COM1";
runCommand(new String[]{ArduinoPath, "-C"+confPath,"-v","-pATmega2560","-cwiring","-P"+port,"-b115200","-D","-Uflash:w:"+hexPath+":i"});

it the same arduino is using but it gives me that error:
avrdude: ser_drain(): read error: The handle is invalid.

It seems highly unlikely that the Arduino is connected to COM1. None of my Arduinos have ever been connected to COM1.

Can you upload to the Arduino using the IDE? Which port does it say the Arduino is connected to?

Have you tried using avrdude directly via the command line first?

DrMessina:
i usei use this command:
String ArduinoPath = "bin/avrdude";
String confPath ="avrdude.conf";
String hexPath = "C:/Users/AMATAL~1/AppData/Local/Temp/arduino_build_669907/test.ino.hex";
String port = "COM1";
runCommand(new String[]{ArduinoPath, "-C"+confPath,"-v","-pATmega2560","-cwiring","-P"+port,"-b115200","-D","-Uflash:w:"+hexPath+":i"});

it the same arduino is using but it gives me that error:
avrdude: ser_drain(): read error: The handle is invalid.

I am assuming that your menthod runCommand() invokes one of the Java Runtime.exec() methods or uses PocessBuilder.. There are many pitfalls to the use of Runtime.exec (see http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html ) and similar problems exist in ProcessBuilder..

I can see one possible problem with that command but without seeing the full content of method runCommand() I cannot be sure and won't be drawn any further.

P.S. You still haven't said what the restrictions are that stop you using the Arduino IDE. It still sounds to me like an XY problem.

@DrMessina, please do not cross-post. Threads merged.

PaulS:
It seems highly unlikely that the Arduino is connected to COM1. None of my Arduinos have ever been connected to COM1.

Can you upload to the Arduino using the IDE? Which port does it say the Arduino is connected to?

yes i can upload from arduino without any problem. and using the COM1 or any other i have ever change it 3 times or 4.
this is the result, it seems to me like it can't go on after overriding baudrate:

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "C:\Users\Amatalamessinaj\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/etc/avrdude.conf"

Using Port : COM7
Using Programmer : wiring
Overriding Baud Rate : 115200
AVR Part : ATmega2560
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :

Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack


eeprom 65 10 8 0 no 4096 8 0 9000 9000 0x00 0x00
flash 65 10 256 0 yes 262144 256 1024 4500 4500 0x00 0x00
lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00

Programmer Type : Wiring
Description : Wiring
Programmer Model: AVRISP
Hardware Version: 15
Firmware Version Master : 2.10
Vtarget : 0.0 V
SCK period : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "C:\Users\AMATAL~1\AppData\Local\Temp\arduino_build_612438/multiloop_test.ino.hex"
avrdude: writing flash (1642 bytes):

Writing | ################################################## | 100% 0.29s

avrdude: 1642 bytes of flash written
avrdude: verifying flash memory against C:\Users\AMATAL~1\AppData\Local\Temp\arduino_build_612438/multiloop_test.ino.hex:
avrdude: load data flash data from input file C:\Users\AMATAL~1\AppData\Local\Temp\arduino_build_612438/multiloop_test.ino.hex:
avrdude: input file C:\Users\AMATAL~1\AppData\Local\Temp\arduino_build_612438/multiloop_test.ino.hex contains 1642 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.23s

avrdude: verifying ...
avrdude: 1642 bytes of flash verified

avrdude done. Thank you.

MarkT:
Have you tried using avrdude directly via the command line first?

no i didn't ...
i have just try it now and the result is the same failed like i show in previous post i checked every line.

stowite:
I am assuming that your menthod runCommand() invokes one of the Java Runtime.exec() methods or uses PocessBuilder.. There are many pitfalls to the use of Runtime.exec (see http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html ) and similar problems exist in ProcessBuilder..

I can see one possible problem with that command but without seeing the full content of method runCommand() I cannot be sure and won't be drawn any further.

P.S. You still haven't said what the restrictions are that stop you using the Arduino IDE. It still sounds to me like an XY problem.

this is the fonction runcommand:
static void runCommand(String[] cmd){
String s = null;

try {

Process p = Runtime.getRuntime().exec(cmd);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

// read the output from the command
System.out.println("command out:\n");
while ( (s = stdInput.readLine ()) != null) System.out.println(s);
System.out.println("errors (if any):\n");
while ( (s = stdError.readLine ()) != null) System.out.println(s);

}catch (IOException e) {
System.out.println("something went wrong: \n");
e.printStackTrace();
}

}
the restriction is that i must create an exe file that upload my sketch and link the javacv project to arduino using Comport fort communication.

So, when the IDE writes to COM7, it is successful. When you use avrdude directly, or from Eclipse, to write to COM1, it fails. And, you can't figure out why.

Well, I can.

PaulS:
So, when the IDE writes to COM7, it is successful. When you use avrdude directly, or from Eclipse, to write to COM1, it fails. And, you can't figure out why.

Well, I can.

hahahaha so funny and usefull !!
i change comport again for test ...

then look this is from arduino:
avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "C:\Users\Amatalamessinaj\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9/etc/avrdude.conf"

Using Port : COM12
Using Programmer : wiring
Overriding Baud Rate : 115200
AVR Part : ATmega2560
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :

Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack


eeprom 65 10 8 0 no 4096 8 0 9000 9000 0x00 0x00
flash 65 10 256 0 yes 262144 256 1024 4500 4500 0x00 0x00
lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00

Programmer Type : Wiring
Description : Wiring
Programmer Model: AVRISP
Hardware Version: 15
Firmware Version Master : 2.10
Vtarget : 0.0 V
SCK period : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "C:\Users\AMATAL~1\AppData\Local\Temp\arduino_build_392831/Final_servos.ino.hex"
avrdude: writing flash (7022 bytes):

Writing | ################################################## | 100% 1.15s

avrdude: 7022 bytes of flash written
avrdude: verifying flash memory against C:\Users\AMATAL~1\AppData\Local\Temp\arduino_build_392831/Final_servos.ino.hex:
avrdude: load data flash data from input file C:\Users\AMATAL~1\AppData\Local\Temp\arduino_build_392831/Final_servos.ino.hex:
avrdude: input file C:\Users\AMATAL~1\AppData\Local\Temp\arduino_build_392831/Final_servos.ino.hex contains 7022 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.88s

avrdude: verifying ...
avrdude: 7022 bytes of flash verified

avrdude done. Thank you.

this is from java:
command out:

errors (if any):

avrdude: Version 6.3, compiled on Dec 16 2016 at 13:33:19
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "C:\Program Files (x86)\Arduino/hardware/tools/avr/etc/avrdude.conf"

Using Port : COM12
Using Programmer : wiring
Overriding Baud Rate : 115200
AVR Part : ATmega2560
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :

Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack


eeprom 65 10 8 0 no 4096 8 0 9000 9000 0x00 0x00
flash 65 10 256 0 yes 262144 256 1024 4500 4500 0x00 0x00
lfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
efuse 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
lock 0 0 0 0 no 1 0 0 9000 9000 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00

Programmer Type : Wiring
Description : Wiring
Programmer Model: AVRISP
Hardware Version: 15
Firmware Version Master : 2.10
Vtarget : 0.0 V
SCK period : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "C:\Users\Amatalamessinaj\AppData\Local\Temp\arduino_build_125106\Final_servos.ino.hex"
avrdude: can't open input file C:\Users\Amatalamessinaj\AppData\Local\Temp\arduino_build_125106\Final_servos.ino.hex: No such file or directory
avrdude: read from file 'C:\Users\Amatalamessinaj\AppData\Local\Temp\arduino_build_125106\Final_servos.ino.hex' failed

avrdude done. Thank you.

now if u can take this a lil bit serious thank you

i correct path directory for conf file that's why i have a different error by now.

avrdude: can't open input file C:\Users\Amatalamessinaj\AppData\Local\Temp\arduino_build_125106\Final_servos.ino.hex: No such file or directory

And this is somehow Eclipse or avrdude's fault?