Run SQL script on PostgreSQL with password supplied - java

I'm trying to run an SQL-script from within my Java app. This is my code:
Runtime rt = Runtime.getRuntime();
rt.exec("setx PGPASSWORD \"" + password + "\"");
String command = String.format("psql -d %s -h %s -p %s -U %s -w -f %s",
database, host, port, user, "create_tables.sql");
System.out.println("Executing command " + command);
Process p = rt.exec(String.format(command));
p.waitFor();
This prints
psql -d routes -h localhost -p 5432 -U postgres -w -f create_tables.sql
as expected.
However within my databases log I see the following:
psql: fe_sendauth: no password supplied
How do I supply the password to psql when calling it as process?

Your environment variable should be set in the same process as the psql is executed. This does not happen in your code as you execute two different processes.
The thing is, that environment variables are local to process contexts.
A new process gets a copy of the parent's environment but each copy is independent.
In your case, the Java process creates child process that modifies PGPASSWORD through setx command - so far so good.
However, this doesn't affect the Java process. Your second child process isn't running in a shell, so it ignores the PGPASSWORD variable as the process is created using OS services. Those services, take into consideration the old context of the Java process which is not aware of PGPASSWORD unless you change the environment in the shell before you start the original (parent) Java process.
Thus,to solve your problem, you have to set up the environment variable and then run the child as a shell command (cmd /c).
To set up environment variable we could follow #Little Santi's advice.
So the code would look something like the following
Runtime rt = Runtime.getRuntime();
String setCommand="PGPASSWORD=" + password;
String command = String.format("psql -d %s -h %s -p %s -U %s -w -f %s",
database, host, port, user, "create_tables.sql");
String finalCommand = "cmd /c \" " + command +"\"";
System.out.println("Executing command " + finalCommand);
Process p = rt.exec(finalCommand ,new String[]{setCommand});
p.waitFor();
Edit 2:

#Plirkee's diagnosis is right: The process in which you start psql misses the required environment variable. But to set it on properly it's better to use the Runtime.exec(String[] cmdarray, String[] envp) method.

Related

Execute external program from Java (with cmd, Windows)

I've done many research for executing an external program (e.g. iTunes) by some simple code, however the suggestions did never work. Sometimes nothing happend, sometimes I got this error message:
English: Unable to find "Discord". Be sure the name is written correctly and try again.
My Code is the following:
try {
String name = (String) "start " + table.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
ProcessBuilder p = new ProcessBuilder("cmd.exe", "cd /D %HOMEDRIVE%%HOMEPATH%/Desktop", "/c", name);
p.start();
} catch (Exception e) {
e.printStackTrace();
}
In my example I get the name of the external program from a JTable, this part is working fine. The ProcessBuilder is changing the directory to the desktop first. Then the external program should be executed by the start <program name> command. With this code I get the mentioned error message.
If you have a solution with cmd, please include changing the directory to the desktop.
You should pass each argument as a single entry to ProcessBuilder. In your current code, you sometimes take multiple arguments together (like cd /D %HOME...). Try passing every argument as it's own parameter, including the command to start and its argument:
String name = (String) table.getValueAt(table.getSelectedRow(), table.getSelectedColumn());
ProcessBuilder p = new ProcessBuilder("cmd.exe", "cd", "/D", "%HOMEDRIVE%%HOMEPATH%/Desktop", "/c", "start", name);
According to: cmd.exe,
/D Ignore registry AutoRun commands
HKLM | HKCU \Software\Microsoft\Command Processor\AutoRun
Did you mean start.exe /D not cmd.exe /D?
and also they told that
If /C or /K is specified, then the remainder of the command line is processed as an immediate command in the new shell. Multiple commands separated by the command separator '&' or '&&' are accepted if surrounded by quotes.
Did you mean cmd.exe /C "cd %HOMEDRIVE%%HOMEPATH%\Desktop & Discord"?

Trying to run cmd commands through Java...only first half is runs

I am running the below query through Java on a Postgres DB using psql:
psql.exe -U <user> -w -h <host> -d <db_name> -a -f <file> 2> "<path_to_file>\psql.log"
Initially, for quite some time the java program did create the file. Then I ran into another problem, that it was not overwriting the log file. So i used file.delete() function after every time this log file got created via java.
Now, Java is not even creating the log file for some reason. If I run the above manually in command prompt, it runs absolutely fine, but not via java code. I can see this command getting run in the java log, but it does not create the log file even when i have removed the file.delete() function
I researched a lot on it but could not find any solution. Any help would be highly appreciated.
its a long code..so i will tell you the relevant part.
I am calling a function from a thread. Code is below for that function:
public static void SaveACopyfileToServer(int auditid,String filepath,String fname,String tb_name,String plpgsql_path) throws Exception
{
Map<String, String> env = System.getenv();
String plpgsql = "\""+plpgsql_path+"\" -U "+env.get("PG_USER")+" -w -h "+env.get("PG_HOST")+" -d "+env.get("PG_DB")+" -a -f "+"\""+filepath+"copy_"+tb_name+auditid+".sql\" 2> \"C:\\ER\\ETL\\logs\\psql.log\"";
System.out.println(plpgsql);
Process p = Runtime.getRuntime().exec(plpgsql);
p.getOutputStream().close();
p.waitFor();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
Calendar cal10 = Calendar.getInstance();
System.out.println("Data loaded for "+tb_name+auditid+" at "+sdf.format(cal10.getTime()));
}
I have tried the following codes also:
String plpgsql = "\""+plpgsql_path+"\" -U "+env.get("PG_USER")+" -w -h "+env.get("PG_HOST")+" -d "+env.get("PG_DB")+" -a -f "+"\""+filepath+"copy_"+tb_name+auditid+".sql\" 2> \"C:\\ER\\ETL\\psql_" +auditid +".log\"";
ProcessBuilder pb = new ProcessBuilder("C:/Windows/System32/cmd.exe",plpgsql);
System.out.println(plpgsql);
Process p =pb.start();
p.getOutputStream().close();
p.waitFor();
and
String filename = filepath+"copy_"+tb_name+auditid+".sql";
String psqllog_file = "C:\\ER\\ETL\\logs\\psql_" +auditid +".log";
Process p = Runtime.getRuntime().exec(new String [] { plpgsql_path,
"-U",
env.get("PG_USER"),
"-w",
"-h",
env.get("PG_HOST"),
"-d",
env.get("PG_DB"),
"-a",
"-f",
filename,
"2>",
psqllog_file });
If i run it through a batch file, then I am getting and error that Windows cannot find -U using the below code in java
String plpgsql = "\""+plpgsql_path+"\" -U "+env.get("PG_USER")+" -w -h "+env.get("PG_HOST")+" -d "+env.get("PG_DB")+" -a -f "+"\""+filepath+"copy_"+tb_name+auditid+".sql\" 2> \"C:\\ER\\ETL\\psql_" +auditid +".log\"";
ProcessBuilder pb = new ProcessBuilder("C:/Windows/System32/cmd.exe","/c","start",plpgsql);
System.out.println(plpgsql);
Process p =pb.start();
p.getOutputStream().close();
p.waitFor();
I am stuck on this for a good amount of time now. Any help would be really really appreciated.
P.S: I am absolutely new to Stackoverflow, still trying to learn how to reply to comments to notify the other person, cz i think my replies are not being sent to the mailbox of others.

Process.start() odd behavior

I am trying to use code to run a popular bitcoin miner.
https://dl.dropboxusercontent.com/u/92716895/DiabloMiner.zip
If you guys know if it, it might be helpful. The thing is that it is a java bitcoin miner. Which needs some dlls to run. The way I manually run it works... which is via cmd going into the directory and typing,
DiabloMiner-Windows.exe -u user -p pass -o server
But when I use the below code to do the same it doesn't work it gives me cannot locate java library path lwjgl.
diabloMinerExe = Path.Combine(storageLocation, "DiabloMiner", "DiabloMiner-Windows.exe");
miner = new Process();
miner.StartInfo.FileName = diabloMinerExe;
miner.StartInfo.Arguments = "-u " + this.user + " -p " + this.password + " -o " + this.server;
miner.Start();
To clarify...
" It's a C# Project that starts a Process which is a .exe which starts a Java based bitcoin miner. "
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/K java -cp libs\\*;DiabloMiner.jar -Djava.library.path=libs\\natives com.diablominer.DiabloMiner.DiabloMiner -u youruser -p yourpassword -o server";
process.StartInfo = startInfo;
process.Start();
This is a working example... I've made it run. If you need help with it, give me a shout ;)
You will need libs folder and DiabloMiner.jar in the directory of your C# app

Java execute debian terminal command with "|"

I am experiencing a really confusing issue about sending commands to terminal via Java.
I have exactly this code:
Process p = Runtime.getRuntime().exec(new String[]{"useradd", server, "-p", pass, "-d", "/home/dakadocp/servers/" + server, "-s", "/bin/false"});
Runtime.getRuntime().exec(new String[]{"echo", server + ":" + pass, "|", "chpasswd"});
The first command is this one "useradd user -p password -d /home/ftp/test/ -s /bin/false" and the second one should be this echo username:new_password | chpasswd, the first command works without any problem and creates the user which I define by the "server" variable, but when I try to execute the second command to change users pass this command probably never happen, output is null and the password is not changed, but when I type this command directly to the terminal it works perfectly so this is just Java issue.
I think the problem is in the character "|", before I tried also some command with "|" and its behavior was the same as with this command. What I am doing wrongly ?
Thanks.
Welite.
| is a shell feature, and requires a shell to run. The trivial fix is to run a shell:
Runtime.getRuntime().exec(new String[] { "sh", "-c", "echo something | chpasswd" });
However, java is more than capable of writing to a process with needing a shell or echo. The better way is to just run chpasswd by itself and writing the string to it:
Process p = Runtime.getRuntime().exec(new String[] { "chpasswd" });
PrintWriter writer = new PrintWriter(p.getOutputStream());
writer.println("foo:bar");
writer.close();

Can't execute command in java

I want one of these commands to be executed. I can't do it in java.
Commands are:
type table1.sql, table2.sql, table3.sql > sBackup.sql
type *.sql > allinone.sql --[Make a backup for all file in the folder]
copy *.sql merged.sql
I tried this:
String command = "type " + "*.sql" + " --result-file=" + "c:\\sBackup.sql";
Runtime.getRuntime().exec(command);
Your valuable insight is highly appreciated. Thank you.
type and copy are both built-ins in the Windows command shell, not actual programs you can execute. To execute them, you execute a command shell and provide the command as an argument after a /C:
String command = "cmd.exe /C type " + "*.sql" + " --result-file=" + "c:\\sBackup.sql";
Runtime.getRuntime().exec(command);
...but I'll just note that I don't think type has a --result-file argument.

Categories