I have developed a demo app in JSFrame which should execute powershell script in my folder.
I am trying following code.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("powershell C:\\helloworld.ps1");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
reader.close();
proc.getOutputStream().close();
}
catch(Exception ex)
{
}
}
And in helloworld.ps1, I am having following command :
$strString = "Hello World 123"
write-host $strString
But I am not getting any output.
Related
I'm trying to run shell script by using ProcessBuilder. The script works but it can't run after the java code. And error stream doesn't output message. I'm running it on centOS 6.9 computer. Please find below my code.
public static ArrayList<String> runCommand(ArrayList<String> command)throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(command);
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
ArrayList<String> commandOutput = new ArrayList<>();
String str;
while((str = reader.readLine()) != null) {
commandOutput.add(str);
}
StringBuilder errorBuilder = new StringBuilder();
while((str = errorReader.readLine()) != null) {
errorBuilder.append(str);
}
String errorMessage = errorBuilder.toString();
if(!errorMessage.equals("")) {
String message = LOG_TAG + ",[runCommand] error:" + errorMessage;
System.out.println(message);
}
reader.close();
errorReader.close();
process.destroy();
return commandOutput;
}
In your case, you are reading something from the output stream of the process, till you consume everything. Then, you try to read error stream.
If the process writes some considerable number of characters on the error stream, the other process will block till they are consumed. To consume both error stream and output stream at the same time, you need to use threads.
You may follow the StreamGobbler technique. You may get some details from that page: https://www.javaworld.com/article/2071275/when-runtime-exec---won-t.html?page=2
This is some code influenced from the page:
public class StreamGobbler extends Thread {
private static final String EOL = System.lineSeparator();
private final InputStream inputStream;
private final StringBuilder output = new StringBuilder();
public StreamGobbler(InputStream inputStream) {
this.inputStream = inputStream;
}
public void run() {
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
) {
String line;
while ((line = reader.readLine()) != null) {
output.append(line);
output.append(EOL);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public String getOutput() {
return output.toString();
}
}
In your code, you use StreamGobbler like this:
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream());
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream());
process.waitFor();
String commandOutput = outputGobbler.getOutput();
String errorMessage = errorGobbler.getOutput();
process.destroy();
I am trying to access the database of another application and print it's contents to the console (USING ROOT), but for some reason I am getting this response:
E/[Error]: Error: incomplete SQL: ls
E/[Error]: exit
I don't really understand why this is happening. I tried to put my syntax in escaped quotations, I tried other sqlite commands, but it doesn't seem to work properly.
I can access the database and print it contents using ADB through my PC with the same path, and if I press the button in my application it asks me for root access, so those 2 are not the issues.
My dumbed down code:
onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCon = (Button)findViewById(R.id.button1);
btnCon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RunWithRoot("su shell -c sqlite3 \"data/data/app.package/databases/Database.db\" \"select * from messages;\"");
}
});
}
RunWithRoot
private void RunWithRoot(String textView) {
try {
String line;
Process process = Runtime.getRuntime().exec(textView);
OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();
stdin.write(("ls\n").getBytes());
stdin.write("exit\n".getBytes());
stdin.flush();
stdin.close();
BufferedReader br =
new BufferedReader(new InputStreamReader(stdout));
while ((line = br.readLine()) != null) {
Log.d("[Output]", line);
}
br.close();
br =
new BufferedReader(new InputStreamReader(stderr));
while ((line = br.readLine()) != null) {
Log.e("[Error]", line);
}
br.close();
process.waitFor();
process.destroy();
} catch (Exception ex) {
}
}
Does someone know what I'm doing wrong? I am very new to Android development, and even newer to root. If someone could throw me in the right direction with this, that would be MUCH appreciated.
PS: This app will be for personal use only, so I don't need checks to see if people have root, or if the path exists, etc.. I already heard a few developer's minds worrying heh
I fixed it using the following code.
private void RunWithRoot(){
String line;
Process process = Runtime.getRuntime().exec("su shell");
OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();
stdin.write("su -c 'sqlite3 \"/data/data/app.pkge/databases/Database.db\" \"select * from messages;\"'\n".getBytes());
stdin.flush();
stdin.close();
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while ((line = br.readLine()) != null) {
Log.d("[Output]", line);
}
br.close();
br = new BufferedReader(new InputStreamReader(stderr));
while ((line = br.readLine()) != null) {
Log.e("[Error]", line);
}
br.close();
process.waitFor();
process.destroy();
}
And now it works correctly! Mostly thanks to #ScaryWombat!
I got the following code that starts a minecraft server:
public class App {
public static void main(String...args) throws Exception {
final ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(new File("C:/Users/trudler/Desktop/New folder (4)/"));
processBuilder.command("java", "-jar", "server.jar");
Process process = processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
I want to do daily backups, so I need to send a "stop" command everyday, to be sure that the files won't be touched while I do the backup (and "start" the server again afterwards).
How can I do this?
I tried it using processBuilder.command("stop"); but that doesn't seem to work.
I think you want to send commands to an existing process, so I think this is what you are looking for:
Execute external program using ProcessBuilder and provide input
public class App{
public static void main(String... args) throws Exception {
while (true) {
Process process = Example.startMinecraft(args);
// Stops for sixty seconds
Thread.sleep(1000 * 60);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
out.write("stop");
// Wait for the process to stop
process.waitFor();
// Now start your Backup
Process backupProcess = Example.startBackup();
backupProcess.waitFor();
// After your backup completed your minecraft server will start again
}
}
private static Process startMinecraft(String... args) throws IOException {
final ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(new File("C:/Users/trudler/Desktop/New folder (4)/"));
processBuilder.command("java", "-jar", "server.jar");
Process process = processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
Thread t1 = new Thread(() -> {
try {
String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// Do something when the Exception is thrown
}
});
t1.start();
return process;
}
private static Process startBackup(){
// Here you have to build your backup process
}
}
If you are on a linux machine I would advise to use some script in /etc/init.d/ instead and use a restart command using this script in a cron job.
I was trying to get the logcat content into a JTextPane. I used following code hoping it will return the content as String but it freeze and also, doesn't produce an error.
Process exec = null;
try {
exec = Runtime.getRuntime().exec("adb logcat -d");
InputStream errorStream = exec.getErrorStream();
BufferedReader ebr = new BufferedReader(new InputStreamReader(errorStream));
String errorLine;
while ((errorLine = ebr.readLine()) != null) {
System.out.println("[ERROR] :- " + errorLine);
}
if (exec.waitFor() == 0) {
InputStream infoStream = exec.getInputStream();
InputStreamReader isr = new InputStreamReader(infoStream);
BufferedReader ibr = new BufferedReader(isr);
String infoLine;
while ((infoLine = ibr.readLine()) != null) {
System.out.println("[INFO] :- " + infoLine);
}
}
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
} finally {
if (exec != null) {
exec.destroy();
}
}
I referred to some tutorials but, they were not filling my problem. Is this wrong? Are there any other methods to get the logcat content as a String programmatically? Sorry if this is a dumb question.
The issue you're seeing is that you're trying to process command streams and wait for the executing process, all in the same thread. It's blocking because the process reading the streams is waiting on the process and you're losing the stream input.
What you'll want to do is implement the function that reads/processes the command output (input stream) in another thread and kick off that thread when you start the process.
Second, you'll probably want to use ProcessBuilder rather than Runtime.exec.
Something like this can be adapted to do what you want:
public class Test {
public static void main(String[] args) throws Exception {
String startDir = System.getProperty("user.dir"); // start in current dir (change if needed)
ProcessBuilder pb = new ProcessBuilder("adb","logcat","-d");
pb.directory(new File(startDir)); // start directory
pb.redirectErrorStream(true); // redirect the error stream to stdout
Process p = pb.start(); // start the process
// start a new thread to handle the stream input
new Thread(new ProcessTestRunnable(p)).start();
p.waitFor(); // wait if needed
}
// mimics stream gobbler, but allows user to process the result
static class ProcessTestRunnable implements Runnable {
Process p;
BufferedReader br;
ProcessTestRunnable(Process p) {
this.p = p;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(p.getInputStream());
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
// do something with the output here...
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
I want to execute android shell command from my android app to execute a uiautomator test jar.
i have tried following options. but neither of them is working for me...
public void execute(String shellcommand) {
Runtime rt = Runtime.getRuntime();
Process p = r.exec(new String[]{"/system/bin/sh", "-c", shellcommand});
}
Also tried...
public void execute(String shellcommand) {
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes(shellcommand + "\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}
Please tell what mistake i m doing?
Android 5.0 solved your problem. Here is new API using which you can execute shell commands.Check here : executeShellCommand (String command)
Enjoy!!!
Try this, i added an output reading process also. But you'll need to cut up your shell command:
ProcessBuilder pb = new ProcessBuilder("adb", "shell", "uiautomator", "runtest", "/data/local/tmp/MyJar.jar", "-c", "com.my.test.Class#testmethod", "-e someparameter someparameterName");
Process pc;
try {
pc = pb.start();
InputStream stdin = pc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
InputStreamReader esr = new InputStreamReader(pc.getErrorStream());
BufferedReader errorReader = new BufferedReader(esr);
pc.waitFor();
} catch (IOException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Assert.fail(e.getMessage());
}