3

I have a Selenium tests written in Java which runs as Jenkins job. I need to have chmod set up to 777 cause otherwise it throws me an error. But I would like to set up chmod properly to somenthing like 775. The problem is that I dont know who is the user which runs the Java tests. I have this code in Java

System.out.println(Runtime.getRuntime().exec("whoami")); 

which returns this: Process[pid=2116487, exitValue=0] Can somebody tell me please what is it? Obviously it is not a user I can set up as owner or as a group of the repository.

Thanks.

2

3 Answers 3

4

To get the output of the process, you need to attach the InputStream of to the normal output of the subprocess. Then, you can read from it by creating a BufferedReader. To get the first line of the output of the process (on stdout) you can use this code:

Process proc = Runtime.getRuntime().exec("whoami"); BufferedReader stdin = new BufferedReader(new InputStreamReader(proc.getInputStream())); String username = stdin.readLine(); System.out.println(username); 

You can also get the InputStream of the error output of the subprocess (stderr) with proc.getErrorStream() if this should be necessary.


However, there is a better way if you want to get the username of the user executing the VM:

String username = System.getProperty("user.name"); System.out.println(username); 
Sign up to request clarification or add additional context in comments.

Comments

1

You are not getting the output of the process "whoami", but the process properties. Try

System.out.println(new Printstream(Runtime.getRuntime().exec("whoami").getOutputStream())); 

7 Comments

java.io.PrintStream
BTW, my IDE (Eclipse) already proposed this namespace. And it showed the Javadoc for the command exec, which shows that the return of the function is the process properties. Your problem could have been avoided by using an IDE.
Sometimes a little break or sleep resolves all problems... ;)
Ok but this command does not write to the console output.
Thanks, I had a typo in the code (the best place for typos...)
|
0

Do you want this?

System.getProperty("user.name") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.