Q: File path and special characters error when Runtime.getRuntime().exec() in java I am trying to launch Apache Spark job from java code. Have problems with path to file and " symbols in it. The code
            String command = sparkPath
                + "bin/spark-submit --class ClassName \"" + curFilePath
                + "\" " + readFilePath + " " + saveFilePath;

        System.out.println("Next command: \n " + command);
        // run Unix command
        Process proc = Runtime.getRuntime().exec(command);

So as a result I have the error:
Exception in thread "main" java.net.URISyntaxException: Illegal character in path at index 0: "/home/ubuntu/Desktop/teh2/first-example-1.0.jar"
at java.net.URI$Parser.fail(URI.java:2848)
at org.apache.spark.util.Utils$.resolveURI(Utils.scala:1343)
at org.apache.spark.deploy.SparkSubmitArguments.parse$1(SparkSubmitArguments.scala:338)

However if I remove " around curFilePath or copy the command from System.out.println and paste/execute it in the command line it works fine with double quotes.

A: Please check from which directory it's getting invoked.
You can try below steps


*

*Add SPARK_HOME and SPARK_PATH in $PATH, if you add it, you can execute the scripts in the bin directory without absolute/relative path

*Runtime.exec is not recommended. Use ProcessBuilder to execute commands, it'll accept a String array/List, add your command and arguments in the list and use it to execute.

