I want to create database import using .sql file with java then I found a code something like this
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(new String[]{"/bin/bash","-c","mysql -p -h localhost test < "+fileName.toString()});
I’m using netbean to run my desktop app. then I got this error message
java.io.IOException: Cannot run program "/bin/bash": CreateProcess error=2, The system cannot find the file specified
my question is where can i find /bin/bash path? or what should i do…
should I configure something like env variable path?
I’m running this on windows
solution is replacing /bin/bash with cmd.exe and -c to /c, but when I execute the program I got this message appear on my console
‘mysql’ is not recognized as an internal or external command,
operable program or batch file.
though I already setup mysql directory in PATH environment variable of Windows
/bin/bashdoesn’t exist on Windows. Try replacing/bin/bashwithcmd.exe, and replacing the switch-cwith/c.EDIT: if your Java program appears to complete successfully but no data has been written, it is quite possible that your Java program didn’t wait for the
mysqlprocess to complete. Try addingpr.waitFor();.Alternatively,
mysqlcould be reporting an error message or writing something to its standard output or standard error streams. If this is the case, you’ll need to either:NUL.You can redirect standard output to
NULby adding>NULto the command line, and redirect standard error toNULby adding2>NUL.I woudn’t recommend discarding the output/error messages. If there’s an error, how will you know about it? However, it’s difficult to properly handle the standard output and standard error streams of processes generated using
Runtime.getRuntime().exec(...). Instead, I would use aProcessBuilder. A ProcessBuilder allows you to redirect themysqlprocess’s standard error into the standard output, which makes reading the output from both streams a bit easier (you don’t need two separate threads).