noobCoder
noobCoder

Reputation: 389

The filename, directory name, or volume label syntax is incorrect java

So i'm currently trying to add a Security Certificate that i have to the Java Keystore. I can do this easily on cmd using the value stored inside the String temp, but I want to be able to do this inside my java code. I'm using the following code to do it, but I get the following error The filename, directory name, or volume label syntax is incorrect.

    String java_Home = System.getProperty("java.home");
    java_Home = java_Home + "\\lib\\security";
    java_Home = java_Home.trim();

    String temp = "..\\..\\bin\\keytool -import -trustcacerts -keystore cacerts -storepass changeit -noprompt -alias RandomKeyName -file C:\\Users\\Chris\\Desktop\\RandomKey.crt";
    temp = temp.trim();

    String[] commands = {"cmd.exe", "/c", "cd " + java_Home, temp};

    ProcessBuilder builder = new ProcessBuilder(commands);
    builder.redirectErrorStream(true);
    Process p = builder.start();
    builder.command();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {
        line = r.readLine();
        if (line == null) {
            break;
        }
        System.out.println(line);
    }

I have looked up this error and found the following page on the error https://technet.microsoft.com/en-us/library/cc956689.aspx., but i still haven't been able to solve the problem. I know it is something small but I just can't see it. Does anyone know how to fix this?

Upvotes: 2

Views: 6935

Answers (2)

TmTron
TmTron

Reputation: 19411

  • use ProcessBuilder.directory to set the working dir (to the location where the keytool is) and then
  • in the constructor use keytool as the command and then the arguments to that program separately

the class documentation of the ProcessBuilder has a good example

Upvotes: 6

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299048

String temp = "..\\..\\bin\\keytool -import -trustcacerts -keystore cacerts -storepass changeit -noprompt -alias RandomKeyName -file C:\\Users\\Chris\\Desktop\\RandomKey.crt";

This is actually not one command, but a whole list of commands. Add a separate array entry for each of the flags and you should be fine. Also, I don't think you need to wrap this inside a call to cmd.exe.

Upvotes: 1

Related Questions