I want to execute sql script using java Java program is:
import java.io.*;
public class script{
public static void main(String argv[]) {
try {
String line;
Process p = Runtime.getRuntime().exec ("psql -U mit -d d980 -h tpeux250.sgp.st.com -f C:/java program/script.sql");
BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
}
but i am getting the following errors
java.io.IOException:Cannot run program "psql": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start<Unknown Source>
at java.lang.Runtime.exec<Unknown Source>
at java.lang.Runtime.exec<Unknown Source>
at java.lang.Runtime.exec<Unknown Source>
at script.main<script.java:8>
The location where the actual psql
binary is isn't on your PATH
environment variable.
To verify run the following command from your terminal.
echo %PATH%
If the psql
bin directory is missing, add it to your path as follows
set PATH = %PATH%;"C:\Program Files\PostgreSQL\9.2\bin"
and try to run your psql
script from the terminal again. If that solves your problem, add the bin
directory permanently (you'd have to since your Java program is sort of running in a different terminal)
My Computer > Properties > Advanced system settings (in the left pane). Select Environment Variables at the bottom and modify PATH to add your psql "bin" directory after a ";" at the end.
Before you run your command from java try executing it in the terminal. I see C:/java program and hoping it is Windows go to run(windows+R) and execute cmd
. In the terminal try executing your command. In windows try back slash instead of forward slash
C:\>cd "c:\Program Files"
is successful whereas
C:\Program Files>cd "c:/Program Files"
The system cannot find the path specified.
is not. So use C:\\java program\\script.sql
用这个
Process p = Runtime.getRuntime().exec ("cmd.exe /c psql -U mit -d d980 -h tpeux250.sgp.st.com -f C:/java program/script.sql");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.