1

While trying to automate an installation process in Ubuntu 16.04 with a .jar file, I did not know how to solve the following problem:

Problem description

Part of the installation files automatically generate the following file called generate in location /usr/share/taskd/pki/:

generate                                                                                                                  
#!/bin/sh

# For a public or production server, purchase a cert from a known CA, and skip
# the next step.

# For development, testing and personal server management, create a CA key and
# cert, and use that to generate a server key and cert.  Creates:
#   ca.key.pem
#   ca.cert.pem
#   server.key.pem
#   server.cert.pem

./generate.ca
./generate.server

# Generate a certificate revocation list (CRL).  The initial CRL is empty, but
# can grow over time.  Creates:
#   server.crl.pem

./generate.crl

# The above is sufficient to operate a server.  What follows is a per-client
# process.  Creates:
#   ${1}.key.pem
#   ${1}.cert.pem

./generate.client client

I have a java project that executes the installation commands,which I run from location /mnt/c/example folder/setup.jar. The java code that runs the command consists of: p = Runtime.getRuntime().exec(String[] command) and the actual command under consideration is created by:

commands[10] = new String[2]
commands[10][0] = "sudo";
commands[10][1] = "/usr/share/taskd/pki/generate";

Whereas the original shell command is:

cd /usr/share/taskd/pki
sudo ./generate

To solve my xy-problem of running the ./generate file, I can

  1. Create it in java with absolute paths, (copy it to the /usr/share/taskd/pki/ folder) and run it.
  2. Create a shell command that actually executes the commands, instead of a .jar file.

Question

However, I was wondering how I can pass the "current directory" of /usr/share/taskd/pki/ to the shell script named generate in that location, such that the ./generate.ca, ./generate/server, ./generate.crl and ./generate.client client are executed correctly/found.

Attempts

Initially I tried to cd to root and from root to /usr/share/taskd/pki/ with the .jar file, however I learned that the cd command is not effective when used from within the .jar.

Doubts

This might seem like purely a programming question which should be posted in Stack Overflow, but I think the essence is platform specific; how to pass environments/paths in Ubuntu between 2 scripts in different locations. I currently cannot predict whether the final answer will be generally applicable to Linux or Ubuntu 16.04. If this perspective or assumption is incorrect please let me know.

  • @DKBose thank you, that was indeed a typo that I corrected. – a.t. Apr 11 at 10:31
  • 1
    So you essentially want to change the working directory of the process started from java? – danzel Apr 11 at 11:00
  • Ah, thank you, assuming it is that process started from java, that also contains/runs the .generate, then that is indeed what I am trying to do. I was not aware of the difference between the working directory and the current directory. I will research again using the "working directory" terminology. With an emphasis on the containment within the .jar script. So essentially what you said, without running scripts before, after or in between the execution of the .jar script (with the exception of the .generate script). – a.t. Apr 11 at 11:06
2

Generally, you should use java.lang.ProcessBuilder instead of Runtime.exec().

ProcessBuilder has some nice features like the ability to modify environment variables or setting the working directory.

Here an example:

List<String> command = new ArrayList<String>();
command.add("./generate");

File workingDirectory = new File("/usr/share/taskd/pki/");

ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(workingDirectory);
Process p = pb.start();
  • Thank you very much for your suggestion. I am a bit confused though, since I was about to close this post as a duplicate of: stackoverflow.com/questions/840190/… thanks to your previous comment yielding a new research therm "working directory". In that post, the main answer states that it is not possible. I will work on implementing your solution, but could you perhaps explain the discrepancy between possibility of your solution, and the claimed impossibility in the referred solution? – a.t. Apr 11 at 11:17
  • The OP in the question you found asked for changing the working directory of the JVM. The reason was that instantiating a new File("relative/path") will result in a file path relative to the working directory of the JVM. If that File was created running /absolute/path/jar.jar, the resulting absolute file path would be /absolute/path/relative/path. Changing the working directory of the currently running JVM is apparently not possible. You, on the other hand, want to start a new Process in a specific working directory, which is perfectly possible. – danzel Apr 11 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.