ENTRY

"Simple java program to run bash under windows. Example with cygwin"

Date:July 7th, 2014
Tags:
Comments: 0
  • Join date: 11-30-99
  • Posts: 12
View Counter:
  • 632 views
Rate it
Ad
Poll
  • What are your favorite build tools?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

I work on windows but want a Java program which will use bash of target systems. They run Linux distros. To test, I use

Given, you have any bash.exe version, i.e. with Cygwin. In my case it is located here D:/programs/shell/cygwin/bin/bash.exe.
Using java version is “1.8.0_05”.

devops/Main.java :

package devops;

public class Main {

	public static void main(String[] args) {
	    // Just a test, put your unix shell command here
		BashRunner.run("ls -alh");
	}

}
 

devops/BashRunner.java :

package devops;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.logging.Logger;
import java.util.logging.Level;

public class BashRunner {
	private static final Logger log = Logger.getLogger(BashRunner.class
			.getName());

	public static void run(String comand) {

		Runtime run = Runtime.getRuntime();

		try {
			String[] env = new String[] { "path=%PATH%;D:/programs/shell/cygwin/bin/" };
			Process proc = run.exec(new String[] { "bash.exe", "-c", comand },
					env);
			proc.waitFor();
			BufferedReader br = new BufferedReader(new InputStreamReader(
					proc.getInputStream()));
			while (br.ready())
				System.out.println(br.readLine());
		} catch (Exception e) {
			log.log(Level.SEVERE, "failed " + comand + " ", e);
		}
	}
}

compile, run it. In my case, I get smth like :

total 12K
drwxr-xr-x  5 diuw Administratoren 4.0K Jul  7 11:59 .
drwxr-xr-x 11 diuw Administratoren 4.0K Jul  7 11:59 ..
-rw-r--r--  1 diuw Administratoren 1.4K Jul  7 11:59 .classpath
-rw-r--r--  1 diuw Administratoren  558 Jul  7 11:59 .project
drwxr-xr-x  2 diuw Administratoren    0 Jul  7 11:59 .settings
-rw-r--r--  1 diuw Administratoren  366 Jul  7 11:59 pom.xml
drwxr-xr-x  4 diuw Administratoren    0 Jul  7 11:59 src
drwxr-xr-x  4 diuw Administratoren    0 Jul  7 11:59 target

Be Sociable, Share!
 
Interesting entry?
VN:F [1.8.5_1061]
Rating: +1 (from 1 vote)
1 votes 'YES'  0 votes 'NO'


TAGS

RELATED
Pages
Posts

SOCIAL
Be Sociable, Share!

INCOMING SEARCH TERMS



Responces

Leave a Reply