3

I know. There are several questions about it and you will say I must use ProcessBuilder and pass a String[] as parameters. But I REALY need to pass a quoted string to exec().

 List<String> args = new ArrayList<String>();
 args.add( "ogr2ogr" ); 
 args.add( "-append" ); 
 args.add( "-update" ); 
 args.add( "-f" );
 args.add( "PostgreSQL" );
 args.add( "PG:\"host='odisseu-db' user='postgres' dbname='odisseu' password='guesswhat' port='5432'\"" ); 
 args.add( file.getName() );
 args.add( "-nlt" ); 
 args.add( "GEOMETRY" ); 
 Process process = new ProcessBuilder( args ).start();

As you can see, the PG parameter needs to be followed by a quoted string. I get an error like: "You dumb! I can't connect to the user='postgres' dataset! ". I'm sure the ogr2ogr is thinking I'm not quoting that parameter.

How can I escape this parameter?

EDIT Updated the parameters one by index. Still same problem.

asked May 6, 2019 at 14:26
5
  • can you execute this command from the command line including here unescaped double quotes? Commented May 6, 2019 at 14:34
  • The UNIX way, -f is one argument, PostgreSQL is another; and any quotes which would be interpreted and removed by the shell (which is almost all of them) aren't supposed to be included in your execve array at all. Commented May 6, 2019 at 16:02
  • 1
    That is to say, if at a command line you would run -f Postgresql "PG:\"host='odisseu-db' user='postgres'\"", you would add -f, Postgresql and PG:"host='odisseu-db' user='postgres'" as separate strings (no more backslashes except as Java syntax, and the outside quotes that are there for consumption by the shell being gone). That's in the UNIX world; if you're using Windows, things are different; the target platform should really be specified in the question to disambiguate. Commented May 6, 2019 at 16:04
  • ...also, you should really provide the working UNIX command line you're trying to emulate as part of the question. Otherwise, we don't know what quotes were there in the original and which quotes you added/changed trying to munge it for Java. Commented May 6, 2019 at 16:04
  • @diginoise yes. Exactly same command in shell run like a charm. Commented May 6, 2019 at 17:35

1 Answer 1

5

Here's the problem. The handling of quotes in the Windows implementation of Java Process is a bit odd.

Any double-quote character that it sees is simply removed. If you want the double-quote to be passed through it has to be escaped with a backslash. And since you are using a Java String literal, the backslash also needs to be escaped.

So ... change the string to this:

"PG:\\\"host='odisseu-db' user='postgres' dbname='odisseu' password='guesswhat' port='5432'\\\""

Reference: https://bugs.openjdk.java.net/browse/JDK-8131908

But note the final comment from the reviewer:

"The reported behavior seems to be in line with this document https://msdn.microsoft.com/en-us/library/17w5ykft.aspx which describes, how the quotation marks are processed in Windows.

Closing this as 'not an issue'."


For Java on Linux, all characters in the argument strings are translated to the OS default character set and then passed straight through to the command. Single or double-quote characters have no special meaning.

answered May 6, 2019 at 15:44
Sign up to request clarification or add additional context in comments.

2 Comments

Granted, "single or double-quote characters have no special meaning" when passed through the UNIX execve() interface, but that doesn't mean the OP correctly translated their quoted-for-UNIX strings (if in fact they have any) to be correct Java literals.
I'm working on Linux and still having same issue. Solved after I create a script file with my command and passing just the file name as a parameter. No trouble anymore and decoupling as a bonus!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.