I'm trying to execute a bcp command like below in linux using java:
bcp dbname..mytable in /home/guest/test -U guest -P guest -S LXXDB1D06 -I /opt/sybase/08/interfaces -c -Y -t \| -r \\n
In Java Class, I do as:
Runtime rt = Runtime.getRuntime();
Process p;
try {
p = rt.exec("bcp dbname..mytable in /home/guest/test -U guest -P guest -S LXXDB1D06 -I /opt/sybase/08/interfaces -c -Y -t \| -r \\n");
p.waitFor();
} catch(..){
..}
I tried with \|, "\"+"\n" and other few.
But didnt work.
What should be the correct format?
Edit: With ProcessBuilder, it looks like:
[bcp, pfactdbcpusdev01..gb_inactive_upc, in, C:\hs_data_dev_00\itm_mstr\tmp\usaupcinact_tmp_rollup, -U, inactupcUSD1, -P, inac01, -S, ACNLNXQ002D01, -I, C:\Program Files\Nielsen\Sybase\12.5.1/interfaces, -c, -t, |, -r, \n, -Y]
This is a super classical problem and I'm frankly astonished that you didn't find the solution by googling around...
The solution is to use a ProcessBuilder:
final ProcessBuilder pb = new ProcessBuilder("bcp", "dbname..mytable",
"in", "/home/guest/test",
"-U", "guest",
"-P", "guest",
"-S", "LXXDB1DO6",
"-I", "/opt/sybase/08/interfaces",
"-c",
"-Y",
"-t", "|",
"-r", "\\n"
);
final Process p = pb.start();
Also, you should check the result of .waitFor().
Also check the manpage for execve(2), execl(2) and friends.
Related
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.
I need to schedule a windows job to loop thru thousand of files and execute a command with the command options, the file name, and extension.
These files have an extension of *.xls in directory c:\proj. For example, one of the file is myImportantFile0001.xls, I would execute the following in powershell, manually:
PS> java.exe -classpath "C:\PROGRA~2\my1.0.2\lib/*" com.my.madsci -v --collector-url https://api.fun.my.com/ --oauth-url https://api.my.com/oauth/access --type /fileName:string/discovery:string --key /fileName:"myImportantFile0001/discovery:discovery" "C:\proj\myImportantFile0001_xls.txt"
couple of problems I've got are:
the command itself has double quotes,
the file name and extensions need to be found and converted
execute a java within powershell
So how can I loop thru the thousand of files to build a string to execute the command using powershell?
does below script helps you? i think it just strings combination.
$Commands = ls C:\Temp\*.xlsx | %{
"java.exe -classpath `"C:\PROGRA~2\my1.0.2\lib/*`" com.my.madsci -v --collector-url https://api.fun.my.com/ --oauth-url https://api.my.com/oauth/access --type /fileName:string/discovery:string --key /fileName:`"$($_.BaseName)/discovery:discovery`" `"C:\proj\$($_.BaseName)_xls.txt`""
}
$Commands
exit # remove the line if you want to execute
$Commands | %{
& $_
}
What about
PS> ls C:\Temp\*.xlsx | foreach-object {
"java.exe -classpath `"C:\PROGRA~2\my1.0.2\lib/*`" com.my.madsci -v --collector-url https://api.fun.my.com/ --oauth-url https://api.my.com/oauth/access --type /fileName:string/discovery:string --key /fileName:`"$($_.name)/discovery:discovery`" `"C:\proj\$($_.name)_xls.txt`""
}
You can do it like this:
$files = Get-ChildItem "C:\temp" -Filter "*.xls"
foreach ($file in $files)
{
$command = "java.exe -classpath ""C:\PROGRA~2\my1.0.2\lib/*"" com.my.madsci "
$command = $command + "-v --collector-url https://api.fun.my.com/ "
$command = $command + "--oauth-url https://api.my.com/oauth/access "
$command = $command + "--type /fileName:string/discovery:string "
$command = $command + ("--key /fileName:""{0}/discovery:discovery"" " -f $file.BaseName)
$command = $command + ("""C:\proj\{0}_xls.txt""" -f $file.BaseName)
Invoke-Expression $command
}
Additionally, you can also schedule it from powershell itself (Works with PS 3.0 and above)
$trigger = New-JobTrigger -Daily -At "9:00 AM"
$scriptPath = "C:\FilePath\myScript.ps1"
Register-ScheduledJob -Name "MyJob" -FilePath $scriptPath -Trigger $trigger
i try to run ffmpeg out java. here my code:
String[] temp = {"ffmpeg\\ffmpeg.exe","-i","input_track.ac3","-threads","0","-af","volume=volume="0.0"dB","-acodec","pcm_s32le","-ac","6","-ar","48000","-f","wav","-","|","ffmpeg\\fdkaac","--ignorelength","-m","1","-o","ouput_track.aac","-"};
ProcessBuilder pb = new ProcessBuilder(temp);
Process p = pb.start();
int ev = 0;
if (p.waitFor() != 0)
{
ev = p.exitValue();
}
i try the comand at windows cmd, here have a problem with "|" at the ffmpeg command line.
maybe someone say my fould?
best regards
This question is similar to How to make pipes work with Runtime.exec()? ... except that it is for Windows.
The problem is essentially the same: the exec methods don't understand shell syntax such as pipes, input or output direction and so on. The solution is essentially the same too: exec the appropriate shell and get that to handle the shell syntax.
In this case, try something like this:
String[] temp = new String[] {
"cmd", "/c",
"ffmpeg\\ffmpeg.exe -i input_track.ac3 -threads 0 " +
"-af volume=volume=\"0.0\"dB -acodec pcm_s32le -ac 6 " +
"-ar 48000 -f wav - | " +
"ffmpeg\\fdkaac --ignorelength -m 1 -o ouput_track.aac -"
};
Note that the actual command is a single string. (The quotes around the 0.0 look a bit strange, but that is what you have in your question.)
| is a shell pipe character, in java you'll have to either run this command in a shall (bash -c "the whole commandline | goes here"), or you'll have to run two processes (the one before the | and the one after), where the stdout of the first writes into the stdin of the second. For this, you'd typically use redirectOutput(Redirect.PIPE) and redirectInput(Redirect.PIPE).
I am trying to create a shell script from java code. I am using following method to create the shell script.
String cmd = "echo -e \"echo -e abc\\\0177\\\0177\\\0177\\\0177\\\0177\\\0177\\\0177\\\0177\" > ";
String [] commands = { "/system/bin/sh", "-c", cmd + "script.sh " };
Runtime.getRuntime().exec(commands);
However, when I open the script.sh file I see following :
echo -e abc\*7\*7\*7\*7\*7\*7\*7\*7
What I want is the following:
echo -e "abc\0177\0177\0177\0177\0177\0177\0177\0177"
What is wrong with I am doing ? Any idea how to fix this ?
Not sure why you need this but you can use single quote in echo and 2 backslashes:
String cmd =
"echo -e 'echo -e abc\\\\0177\\\\0177\\\\0177\\\\0177\\\\0177\\\\0177\\\\0177\\\\0177' > ";
I'm trying to execute unix commands thru a java program. Some of these commands involve an if-then-fi statement. Can this be done thru java / Runtime class? Seems like it only handles 1 command at a time.
I'm looking to do something like this:
grep 'Error One' SystemErr.log > $HOME/tempFiles/output.txt
grep 'Error Two' SystemErr.log >> $HOME/tempFiles/output.txt
grep 'Error Three' SystemErr.log >> $HOME/tempFiles/output.txt
.
.
if [ -s $HOME/tempFiles/output.txt ]
then
mail -s "Subject here" "a#b.com" < $HOME/tempFiles/output.txt
fi
Basically, I just want to email the file (results) if the grep found anything.
I want to use java instead of a direct shell script so that the errors I search for can be database-driven, easier to change.
I know I could read the file myself in java and search/parse it myself. But grep and other unix commands have a lot of built-in functionality I want to use to make it easier.
Any ideas, or am I totally on the wrong track?
Here is some code, using simpler commands, but basically equivalent:
public static void main( String[] args ) throws Exception {
try {
ProcessBuilder pb = new ProcessBuilder( "/bin/bash", "-c",
"echo one >/tmp/xxx && echo two >>/tmp/xxx && " +
"if [ -s /tmp/xxx ]; then cp /tmp/xxx /tmp/yyy; fi" );
File log = new File( "/tmp/log.txt" );
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
Process process = pb.start();
process.waitFor();
} catch( Exception e ){
// ...
} catch( Error e ){
// ...
}
}
The trick is to put it all into a single shell command so that you can call /bin/bash with the -c command option.
If composing this command is too complicated, write a shell file and source that.