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.
Related
I am trying to read a file and then take the contents of that file and have it executed as user input. I am using Scanner for reading files and user input but I am not sure if this is the correct way to go about this since Scanner for input can only System.in and so I am not sure how to pass data from file into input scanner for it to execute in the console. This is my code below for reading class
public class readingFile {
Scanner fileReading = new Scanner(new File("somecontent.txt"));
Scanner input = new Scanner(System.in);
public readingFile() throws FileNotFoundException {
}
public void startReading()
{
System.out.println("reading file...");
while(fileReading.hasNextLine()){
String data = fileReading.nextLine();
System.out.println(data);
Scanner input = new Scanner(System.in);
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class readingFile {
static String javaFileFullPath = "D://myfolder/Program.java";
public static void main(String[] args) {
executeJavaFile();
}
public static void executeJavaFile() {
try {
System.out.println("executing java program from file....");
Process compileProcess = Runtime.getRuntime().exec("cmd /c javac "+javaFileFullPath);
Thread.sleep(5000);
System.out.println(compileProcess.exitValue());
BufferedReader inputReader = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
String line = "";
while ((line = inputReader.readLine()) != null) {
System.out.println(line);
}
inputReader.close();
Process runProcess = Runtime.getRuntime().exec("cmd /c java "+javaFileFullPath);
Thread.sleep(5000);
System.out.println(runProcess.exitValue());
BufferedReader inReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
String lineStr = "";
while ((lineStr = inReader.readLine()) != null) {
System.out.println(lineStr);
}
inReader.close();
} catch (Exception ex) {
System.out.println("Exception:"+ex.getMessage());
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class readingFile {
static String javaFileFullPath = "D://myfolder/Program.java";
public static void main(String[] args) {
executeJavaFile();
}
public static void executeJavaFile() {
try {
System.out.println("executing java program from file....");
Process compileProcess = Runtime.getRuntime().exec("cmd /c javac "+javaFileFullPath);
Thread.sleep(5000);
System.out.println(compileProcess.exitValue());
BufferedReader inputReader = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
String line = "";
while ((line = inputReader.readLine()) != null) {
System.out.println(line);
}
inputReader.close();
Process runProcess = Runtime.getRuntime().exec("cmd /c java "+javaFileFullPath);
Thread.sleep(5000);
System.out.println(runProcess.exitValue());
BufferedReader inReader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
String lineStr = "";
while ((lineStr = inReader.readLine()) != null) {
System.out.println(lineStr);
}
inReader.close();
} catch (Exception ex) {
System.out.println("Exception:"+ex.getMessage());
}
}
}
import java.io.*;
public class Clasa {
public static void main(String args[]) {
try {
Runtime rt = Runtime.getRuntime();
String comand = "net start MySQL55";
Process pr = rt.exec(" cmd.exe /C " + "net start MySQL55");
BufferedReader input = new BufferedReader(
new InputStreamReader(pr.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
I try this and gives me the next error code: Exited with error code 2
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
I am trying to run an external process in Java and I have no idea why my code isn't working. It works for any other 'cmd' command (for example /c dir). If I replace cmd sc sdshow w32time with cmd /c dir it works.
Here is my code:
public class services2 {
public static void main(String args[]) {
try {
Process p = Runtime.getRuntime().exec("cmd sc sdshow w32time");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
} catch (IOException e1) {
} catch (InterruptedException e2) {
}
System.out.println("Done");
}
}
Any ideas?
Your problem seems to be that sc is not an argument for cmd.
What you need is:
sc sdshow w32time
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