Hi i have a shell script named script .sh that create maven project having parent pom and multiple modules under it as follows:

 #!/bin/bash 
 mvn archetype:generate -DgroupId="com.low.co" -DartifactId="tool-release" -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE
cd yeti-release
 mvn archetype:generate -DgroupId="com.low.cp" -DartifactId="tool-api" -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE
 mvn archetype:generate -DgroupId="com.low.cp" -DartifactId="tool-impl" -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE
 mvn archetype:generate -DgroupId="com.low.cp" -DartifactId="tool-rest" -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE
 mvn archetype:generate -DgroupId="com.low.cp" -DartifactId="tool-service" -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE 

I want to run this script from java code i am using mac osx i tried every thing my code that is opening this script is as follows:

   final File scriptDirectory = new File("src/main/resources/");  
   final String shellScript = "./script.sh"; 
   ProcessBuilder pb = new ProcessBuilder();
   pb.command("/bin/bash","-e",shellScript);
   pb.directory(scriptDirectory); 
   p=pb.start();

when i change script.sh it is not showing console nor executing any of the commands in the shell script
any suggestions pls

Dani AI

Generated

A few likely causes and a focused checklist for (and notes on the previous replies from and ):

The behaviour you describe is almost always one (or more) of these: the script never runs where you expect because of a wrong cwd or a typo (your script creates tool-release then does cd yeti-release), the mvn binary is not found because the Java process has a different PATH on macOS, or the child process’s IO is not attached/consumed so output never appears and the process can block. ’s Runtime.exec approach can work, but is right that ProcessBuilder is the better API — it just needs the right configuration.

Quick checklist (do these in order):

  • Run the script by hand in Terminal first (use bash -x script.sh to trace). Confirm the directories that the script expects actually exist.
  • Make the script executable and fix line endings if needed: chmod +x script.sh and ensure LF (not CRLF).
  • Force Maven to run non-interactively if you don’t want prompts: add Maven batch mode (-B) or -DinteractiveMode=false.
  • Ensure the Java-launched process can find mvn: either call the absolute path to the mvn binary or extend the ProcessBuilder environment PATH.
  • Attach or consume IO: either inherit the JVM terminal or stream the output in Java; otherwise nothing will show and buffers may block.

Example patterns (do not copy OP’s script contents — these are just the launcher patterns):

ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-lc", "/absolute/path/to/script.sh");
pb.directory(new File("/absolute/path/to/working/dir"));
Map<String,String> env = pb.environment();
env.put("PATH", env.get("PATH") + ":/usr/local/bin"); // adjust where mvn lives
pb.redirectErrorStream(true);
pb.inheritIO(); // makes child use the same console as the Java process
Process p = pb.start();
int exit = p.waitFor();

If a Terminal window is required on macOS for interactive work, run the script with AppleScript so a real Terminal opens:

osascript -e 'tell application "Terminal" to do script "/absolute/path/to/script.sh"'

Typical root causes here are the wrong cd target, missing mvn on PATH, or not attaching/consuming IO. Fix those and ProcessBuilder will behave as expected.

Recommended Answers

All 2 Replies

Member Avatar for Member #1111627

A stab in the dark here, but have you tried using RunTime instead? I think you can accomplish the same by (please note that the syntax may be off):

Runtime.getRuntime().exec("/bin/bash -e " + shellscript);

More info HERE.

ProcessBuilder was intended as a replacement for Runtime exec, so maybe that will work, but it is a step backwards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.