package burak;
import java.io.*;
public class Server {
public static void main(String[] args) {
try {
String[] command = new String[2];
command[0] = "cmd";
command[1] = "telnet";
Process p = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String Error;
while ((Error = stdError.readLine()) != null) {
System.out.println(Error);
}
while ((Error = stdInput.readLine()) != null) {
System.out.println(Error);
}
} catch (Exception e) {
e.printStackTrace();
}
}
I want to open telnet and send some commands but ı failed to open telnet what is wrong can you tell me?and ı need some examples about telnet conneciton expcect apache.common because ı have to use many ips in one run and ı dont know how to use args in this condi
Related
I am working on a Server launcher. This launcher runs Minecraft servers.
I want to get colors from the server's process's input like windows command prompt. How can I do that?
My server thread:
serverThread = new RunnableThread("ServerThread-" + serverName) {
#Override
public void onRun() {
if (!getProcess().isAlive()) {
ServerStatusChangeEvent.change(LocalServer.this, StatusType.STOPPED);
closePort();
if(queryTimerTask != null) queryTimerTask.cancel(false);
cancel();
}
try {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(getProcess().getInputStream(), Charset.forName("UTF-8")));
String line;
while ((line = reader.readLine()) != null) {
String l = line;
Platform.runLater(() -> parseLine(l));
}
reader.close();
} catch (final Exception e) {
//empty catch block
}
}
};
Thanks for the answers and sorry for my bad english!
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!
import java.io.*;
public class ColorTest {
public static void main(String [] args){
try{
//Process p = Runtime.getRuntime().exec("cmd /c color 0a");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readline()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am not understanding why the command won't execute. The line that is commented is the one I need help with.
I'm running a Java program from another Java application using Runtime.getRuntime().exec like this
Process p1 = Runtime.getRuntime().exec("javac test.java");
Process p2 = Runtime.getRuntime().exec("java test");
The content of the test.java
import java.io.*;
class test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println(s);
}
}
I want to handle Input, Output and Error stream of the process p2.
I did capture of the output of the test.java, however, I do not know how to handle output and error.
Here is my code:
try {
String s = "";
InputStream istr = p2.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));
BufferedReader bre = new BufferedReader
(new InputStreamReader(p2.getErrorStream()));
while ((s = br.readLine()) != null) {
System.out.println(s);
}
br.close();
while ((s = bre.readLine()) != null) {
System.out.println(s);
}
bre.close();
p2.waitFor();
} catch (IOException ex) {
ex.printStackTrace();
} catch (Exception err) {
err.printStackTrace();
}
The code above works fine for capturing the output of the test.java. But it does not display error of the test.java.
Could you please give me a sample code for fixing this problem and handling output stream or share idea? Thanks in advance
The solution I've always used is to create a separate thread to read one of the streams
So, in your case it should be something like
String s = "";
InputStream istr = p2.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));
BufferedReader bre = new BufferedReader
(new InputStreamReader(p2.getErrorStream()));
new Thread(new Runnable() {
#Override
public void run() {
while ((s = br.readLine()) != null) {
System.out.println(s);
}
}
}).start();
new Thread(new Runnable() {
#Override
public void run() {
while ((s = bre.readLine()) != null) {
System.out.println(s);
}
}
}).start();
// when you are finished close streams