Executing .sh file from java




















Let's use a BufferedReader to take a look at what's going on:. Now, when we run this method after the exec method, it should yield something along the lines of:. Keep in mind that we'll have to extract the process information from the Process instances as we go through other examples. If you'd like to run a command from, say, a certain folder, we'd do something along the lines of:.

Here, we've provided the exec method with a command , a null for new environment variables and a new File which is set as our working directory.

In this case, it's dir. The reason why this wasn't mandatory for the ping example, but is mandatory for this example is nicely answered by an SO user. Let's take a look at how we could supply the previous command in several individual parts, instead of a single String:. Ultimately, regardless of the approach - using a single String or a String array, the command you input will always be broken down into an array before getting processed by underlying logic.

We can supply as many environment variables as we'd like within the String array. Here, we've just printed the value of var1 using echo. Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! Sometimes, it's just much easier to offload everything into a file and run that file instead of adding everything programmatically. Depending on your operating system, you'd use either.

Let's create one with the contents:. This'll open up the command prompt and run the. See Difference between ProcessBuilder and Runtime. When you execute a script from Java it spawns a new shell where the PATH environment variable is not set. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Running a bash shell script in java Ask Question. Asked 9 years, 1 month ago. Active 8 years, 11 months ago. Viewed 84k times.

Improve this question. Bhavik Ambani 6, 14 14 gold badges 52 52 silver badges 86 86 bronze badges. Shane Shane 2 2 gold badges 7 7 silver badges 23 23 bronze badges.

Have a look at ProcessBuilder instead of Runtime. Curious why you are not directly executing nfdump — Miserable Variable. Can you check whether script.

Nope not empty! ProcessBuilder seems to work fine though! Are you sure your file is executable e. Also, consider using getRuntime. Perhaps the shell isn't parsing your command line properly. What's the value of file. Check that the file is being run with a suitable environment path, etc. Try a simple sh file e. TedHopp Will do, could this be anything to do with the fact that I am running this in a shutdown hook?

It's meant to launch another program that will stay open until terminated. This is intended as a server command so that the user can restart the server via a command. Show 1 more comment. Active Oldest Votes. Since you confirmed that the file has executable permission, try to pass the argument as follows: Runtime. Improve this answer.

It seems to be working if in the script I just simply create a directory but it doesn't seem to like the screen command, maybe I need to include or use the exact location of screen? I finally found the error, decided to get it to output to an error log and found this "Must be connected to a terminal.

Add a comment. Best regards. Marcelo Tataje Marcelo Tataje 3, 20 20 silver badges 47 47 bronze badges. I recommend using org. Do you have any example code? I can't seem to find much documentation — Keir Nellyer.



0コメント

  • 1000 / 1000