Too many command-line arguments when calling pg_dump from java - java

After running into an issue on executing some queries as strings in Java for postgres, I went using string arrays, which solved my existing issues.
After the switch I am now having an issue with pg_dump, but not with pg_restore.
When I supply my method with the following array:
[time, ./pg_dump, -U, lehigh, -d, lehigh, -Fc, data/completedDb.dump]
I get the following error:
pg_dump: too many command-line arguments (first is
"data/completedDb.dump")
ProcessBuilder produces the following for my execution:
time ./pg_dump -U lehigh -d lehigh -Fc data/completedDb.dump
And it works fine when I add the output arrow, and remove the data folder, on the command line.
time ./pg_dump -U lehigh -d lehigh -Fc > completedDb.dump
I'm running this through eclipse, in Java on a postgres database, using :
Runtime.getRuntime().exec();
I've tried using Process.start() but got the same errors, so I'm totally dumbfounded at what I'm doing wrong.
Prior to this change, pg_dump was being executed properly as a single string. I don't want to go back to that method as I want to maintain consistency, but I also want to figure out what I'm doing wrong here.

You should use -f before the output file name, since by default pg_dump outputs to stdout.
Try
[time, ./pg_dump, -U, lehigh, -d, lehigh, -Fc, -f, data/completedDb.dump]

Related

Execuring powershell commands from Java

I am trying to execute powershell commands from Java and have tried multiple options.
Commands that I want to try -
$SecureFilePassword = ConvertTo-SecureString -String "<PFXPassword>" -AsPlainText -Force
$userPFXObject = New-IntuneUserPfxCertificate -PathToPfxFile "<FullPathPFXToCert>" -PfxPassword $SecureFilePassword -UPN "<UserUPN>" -ProviderName "<ProviderName>" -KeyName "<KeyName>" -IntendedPurpose "<IntendedPurpose>"
I tried using ProcessBuilder to execute these - but i am not able to maintain session and hence it starts saying cmdlet not known even after importing the corresponding ps1 file.
Then I started with using jPowershell - here everything works fine except the above commands when it tries to use the set value inside $SecureFilePassword inside the next command - it fails saying "Not Specified"
Not sure if I am formulating the command properly to be executed - could some one help me?
Thanks
Sri
I would suggest to write a PowerShell script in a file that could be run in your shell as a single command and after that use ProcessBuilder.start() or Runtime.exec() method to run an external command from Java. See class Process API for details

Using parameters from shell script in my Java code

I have to write two scripts, one to compile my code and another one to run it. I manage to compile the code with this script:
#!/bin/bash
javac SimilaridadeP.java
And I was able to run it using this script:
#!/bin/bash
java SimilaridadeP
The problem is that I need to execute my second shell script following this command:
./execute.sh input output
Where input and output are the names of the files that I am going to use in my Java code. The problem that I am facing right now is that I don't know how I can use these both names in my Java code.
So far I managed to get both names using this code:
#!/bin/bash
in=$1
out=$2
java SimilaridadeP
But since "SimilaridadeP" is the name of my java file and it can't be called like a method with parameters I am kind of lost.
If your java code is already reading the command-line parameters, it's just a matter of changing the bash this way:
#!/bin/bash
java SimilaridadeP "$1" "$2"
This works with arbitrary number of arguments:
#!/bin/bash
java SimilaridadeP "$#"

WEKA classifier arguments from the command line

I'm running ubuntu 14.10, and I've created and saved a model through the GUI. Now I have some test data I'd like to run from the command line. This works fine, as long as I don't try and pass any classifier specific options. When I do, I receive a "invalid option" message.
For example, the following works:
java -Xmx1g -cp /usr/share/java/weka.jar weka.classifiers.lazy.IBk -l ibk1-full.model -T testdata.arff
However, the following does not:
java -Xmx1g -cp /usr/share/java/weka.jar weka.classifiers.lazy.IBk -l ibk1-full.model -T testdata.arff -K 3
Weka exception: Illegal options: -K 3
General options:
-h or -help
Output help information.
-synopsis or -info
...
How does one pass command line arguments to the classifier?
Additional question: are the default arguments used when generating the model saved with the model, so that when someone uses '-l foo.model' to load it from the command line, one does not need to specify the rest of the arguments on the command line? The weka CLI primer documentation is unclear on this.
Note: I know IBk isn't exactly a model, per-se, but its illustrative of every classifier I try.
The -K parameter does not appear to work when a classifier is being loaded, but rather when training the model from the command prompt. This is likely due to the fact that the kNN model is already trained with, say, k=1, so changing k would change the model that has already been generated.
If you use the parameters -K, -t and -T, you should be able to generate a new model with the desired k on a nominated training set and evaluated on given testing data.
I don't believe the issue you have is with the command line arguments (you're doing it right!), but rather that the argument is invalid in your given situation.
Additional Question: The parameters of the model that was trained are not likely required as they were used for training, which was completed before being saved. Other parameters will still be required (like testing data for evaluation).
Hope this Helps!

using '.' when passing parameters to java binary with -D does not parse well

I am using Jenkins CI to run java from execute shell build step :
java -jar -Dadmin.hostname=$hostname -Dschema_name=$schema myapp.jar
I have noticed that although both parameters hostname and schema are defined well and has non empty values on runtime, Jenkins parses it the following way:
java -jar -Dadmin.hostname= -Dschema_name=MYDB myapp.jar
meaning admin.hostname value doesn't get parsed.
I have tracked the problem and noticed that when i dont use . in the parameter name everything get parsed okay.
I am wondering if this is a limitation of Java command line or a bug in Jenkins perhaps. (I am using RHEL64)
Thanks
EDIT:
I think this is bug with Parametrized build jenkins plugin as using . in other builds works fine.
Perhaps the problem is that environment variables on UNIX are case sensitive
$ echo $hostname
$ echo $HOSTNAME
myhostname
There shouldn't be any problem using . because may built in properties uses them.
BTW: Just because a property is set to blank doesn't mean its not set.
System.out.println("not.set=" + System.getProperty("not.set"));
System.out.println("admin.hostname='" + System.getProperty("admin.hostname")+"'");
when run with -Dadmin.hostname= prints
not.set=null
admin.hostname=''

SQLCMD not working when run using Java

This is an extremely strange situation, but I just cannot point out what I'm doing wrong.
I'm executing a big bunch of SQL scripts (table creation scripts, mostly). They are executed through Java, using sqlcmd. Here's the sqlcmd command I use.
sqlcmd -m 11 -S SERVER -d DB -U USER -P PASS -r0 -i "SCRIPT.sql" 2> "ERRORS.log" 1> NULL
Note: I use the -r0 and redirects to make sure only errors go into the log file. I chuck out all STDOUTs.
Now I execute this command in Java, using getRuntime.exec(), like this.
Runtime.getRuntime().gc();
strCmd = "cmd /c sqlcmd -m 11 -S SERVER -d DB -U USER -P PASS -r0 -i \"SCRIPT.sql\" 2> \"ERRORS.log\" 1> NULL"
Process proc = Runtime.getRuntime().exec(strCmd);
proc.waitFor();
Note: I use cmd /c, so that the command runs in its own shell and exits gracefully. Also, this helps in immediately reading the error log to look for errors.
The Problem!
This command works perfectly when run by hand on the command prompt (i.e. the tables are getting created as intended). However, when executed through Java as shown, the scripts are run, and and there are no errors, no exceptions, nothing in the logs. But, when checking in SSMS, the tables aren't there!
Where do I even begin debugging this issue?
UPDATE: I'M A MORON
The return value from the getRuntime().exec method is 1. It should be 0, which denotes normal execution.
Any pointers on how to fix this?
UPDATE 2
I've looked at the process' ErrorStream, and this is what it has.
Sqlcmd: Error: Error occurred while opening or operating on file 2>
(Reason: The filename, directory name, or volume label syntax is
incorrect).
Looks like the path I'm passing is wrong. The error log goes into my profile directory, which is C:\Documents and Settings\my_username. Do the spaces in the path matter? I'm anyways double-quoting them!
Have a look at the exec method with an string array as parameter:
java.lang.Runtime.exec(String[] cmdArray)
The JavaDoc for this method says:
Executes the specified command and arguments in a separate process.
So, the first item in the array is the command and all of your arguments are appended to the array, e. g.,
Runtime.getRuntime().exec(new String[] {"cmd", "/c", "sqlcmd ... "});
After looking at your comment and the implementation of exec(String) it seems to be, that the exec method recognizes the pipe operator > as an argument to cmd, because exec(String) splits the command string to an array using whitespaces as seperators.
I don't have privs to post comments - which is what this is - but what if you try putting in a bogus user id for the DB? Does that cause a different execution path? Will that give you a Java error? Or an Auth error in your DB? Also, def tweak the user, not the password and learn from my experience that if you tweak the password that's a great way to get an account locked out!
The other thing - and this may be a shot in the dark - but what are the JRE and driver you're using? I believe there's a known issue with JRE 1.6.0.29 and the sqljdbc4 JAR. I have more details on this, but I'll have to post the link once I get to work.
Edit:
I know it's been established that the JRE/sqljdbc combo isn't your issue, but if folks search and find this, here is the link I spoke of above:
Driver.getConnection hangs using SQLServer driver and Java 1.6.0_29
First enable log/view commands output (since exec() returns 1), which would point out possible cause of the issue.
Use proc.getInputStream() and print the contents to a file or console.

Categories