Skip to content

Instantly share code, notes, and snippets.

Created October 26, 2011 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1315951 to your computer and use it in GitHub Desktop.
Save anonymous/1315951 to your computer and use it in GitHub Desktop.
I have also tried:
=================
$ touch /tmp/bash.sh
$ chmod +x /tmp/bash.sh
$ echo "sudo convert /tmp/testing.tiff /tmp/testing.jpg" > /tmp/bash.sh
$ java -jar Myshell.jar # note: system("/tmp/bash.sh"); was used as absolute path
[sudo] password for alex:
package installer;
import java.io.*;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Properties;
/* Use: call it from other class as: Myshell.system("sudo convert /tmp/testing.tiff /tmp/testing.jpg");
*/
public class Myshell
{
public static String system(String cmd)
{
String value = "";
try
{
Process p=Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
value += line + "\n\r";
line=reader.readLine();
}
} catch(IOException e1) {
} catch(InterruptedException e2) {
}
return value;
}
}
$ whoami
alex
$ which java
/usr/bin/java
$ which convert
/usr/bin/convert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment