Execute sh file from Java


I am trying to execute an sh file via the following code (CentOS machine btw)

Runtime.getRuntime().exec("sh " + file.getPath());

I use this code for windows and it works fine

Runtime.getRuntime().exec("cmd /c start " + file.getPath());

Could it be because I'm using Screen in the .sh file? I am also using the java command to start the server so maybe I need to include these?

Here are the contents of the sh file

#!/bin/sh BINDIR=$(dirname "$(readlink -fn "$0")") cd "$BINDIR" screen -S PrivateServer java -Xms2048M -Xmx2048M -jar somejar.jar -o true

I am also running this code from a shutdown hook, could this be the issue? This is intended because the software is a game server and it is intended so that the user can use a restart command or have it auto restart without needing to setup anything them self.

Edit: I decided to output the errors to a text file and found this "Must be connected to a terminal." any ideas? I believe this is an issue to do with using screen.


Since you confirmed that the file has executable permission, try to pass the argument as follows:

Runtime.getRuntime().exec(new String[]{"/bin/sh" ,"-c", file.getPath()});

So mainly provide the complete path for sh and use -c


Yo need to set the permissions for the operation, usually the "sh" files require execution permission in order to run this scripts from a Java Application. You must run the Java application as root (sudo su) or sudo java -jar. Or set full access to your script "sudo chmod 777 ..." well, not full access, but at least chmod +x for any user that wants to execute your script.

Best regards.


I recommend using org.apache.commons.exec.* to run such.


It does not seem to have executable permissions, try executing "chmod +x shell_script_filename" before executing the shell script


  • Unity 5.1.1 android “merged manifest” or “classes into dex format” error
  • JRE_HOME not found on pip jnius installation
  • Finding pointer to argv[0] so I can change it
  • How to know the exact location where the file is being accessed in Android NDK
  • Configuring install path: prefix=[PREFIX] not fully understood
  • error while installing ocamlfind.1.3.3 configure: m4 not in PATH; this is required
  • IntelliJ IDEA 15 Scene Builder not showing all controls
  • How to open an internal port in Openshift 3 Online?
  • Bash loop through directory and rename every file
  • Select query with regular expression - MySql
  • Symfony 4 - 3rd-party bundle commands are no longer automatically discovered
  • Visual Studio + grunt - run tasks depending on configuration?
  • Help me understand this C code (*(void(*) ()) scode) ()
  • cakephp select options attributes
  • Setup buildroot for old kernels
  • Expanding root partition on AWS EC2
  • Error when loading .dll from a different directory using python ctypes.CDLL()
  • fatal error: openssl/e_os2.h: No such file or directory
  • Compile ffmpeg as stand alone binary
  • How to close Windows Explorer from CMD
  • How to use variables in npm scripts on windows
  • CakePHP 3 on Bluehost Console Commands Do Not Function
  • Batch: Set one directory up path in variable
  • Is there a way to rename directories recursively without using sed or rename?
  • Ignore some files from SVN commit
  • Modifying files nested in tar archive
  • Getting directory of input file (Applescript)
  • Is it possible to make imports depend on the location of my Lua script instead of the current direct
  • integrate POCO library in android ndk
  • removing last 3 characters on a file (file extension)
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • what does prefix @- mean in makefile?
  • “mvn clean generate-source” could not resolve dependencies
  • In Java, how can I construct a File from a resource?
  • Application level floating views with navigation in Android
  • Android Studio Can't Find tools.jar
  • npm 5.4.1 install/uninstall all failing
  • AndEngine Applying Transparancy to AndEngine View
  • Using a canvas object in a thread to do simple animations - Java
  • Avoid links criss cross / overlap in d3.js using force layout