Listing virtual disks in Java - java

Is there a way to list all virtual disks attached using Java?
I've tried using ProccessBuilder to open diskpart and running command and then using InputStreamReader, save lines into an array to later extract vhd name. But after running diskpart command my program freezes.
try {
commands.add("cmd.exe");
commands.add("start");
commands.add("diskpart");
commands.add("list vdisk");
ProcessBuilder builder = new ProcessBuilder(commands);
Process p = builder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}

Related

How do I run this python script in java?

I want to use this script in my java app: https://github.com/jcapona/amazon-wishlist-scraper.
I've looked around and I tried executing the script like so:
public static void main(String[] args) throws IOException {
String s = null;
Process p = Runtime.getRuntime().exec("python C:\\\\Users\\\\Home\\\\work\\\\test.py");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((s=stdInput.readLine()) != null) {
System.out.println(s);
}
}
But I am not getting any output. What do I need to be able to run this specific script?
You can use like this :
String command = "python /c start python path\to\script\script.py>";
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
while ((line = stdInput.readLine()) != null) {
System.out.println(line);
}
stdInput.close();
while ((line = stdErr.readLine()) != null) {
System.out.println(line);
}
stdErr.close();
p.waitFor();
System.out.println("Done.");
p.destroy();
Protip: Always copy your path from the file explorer tab
And since you are getting JSON response try GSON library to parse it.
And if you want to work heavily on python using java, try exploring Jython

Java InputStream to string

in my java program, i am trying to get the InputStream from a process and print it with this piece of code:
try {
Process p = Runtime.getRuntime().exec("cmd /c start dammage\\4.simulation.cmd");
//BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
//StringBuffer sb = new StringBuffer();
//String line;
//while ((line = br.readLine()) != null) {
//sb.append(line).append("\n");
//}
//System.out.println(sb.toString());
String input = IOUtils.toString(p.getErrorStream());
System.out.println(input);
} catch (IOException ex) {
Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Something happened");
}
I tried both ways shown above (commented and uncommented), but none of them prints anything. So i would like to ask what am i doing wrong here?
I appreciate any help.
The buffered reader solution looks fine. You might be looking in the wrong stream. Try getting from both streams.. Like
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuffer sb = new StringBuffer();
String line;
//Read the output from the command
while ((line = stdInput.readLine()) != null) {
sb.append(line).append("\n");
}
//read any errors from the attempted command
while ((line = stdError.readLine()) != null) {
sb.append(line).append("\n");
}
Are you sure it should print something? Because, the commented code should work just right provided the command executed is returning non-empty input stream. Try replacing the argument of exec to "cmd". And see if it's able to read from the input stream. Do following. On windows machine it should give you welcome message from cmd (the usual welcome message we get after we run start command prompt).
Process p = Runtime.getRuntime().exec("cmd");
About the uncommented code, How IOUtils work? Does it read from the error stream repeatedly. Because, IMO, it's just one time read and not the repetitive one.
Hope I don't confuse.
You should add a p.waitFor(); to give the program time to terminate. Also, verify if you really want to read stdout or stderr
This works for me:
Process p = Runtime.getRuntime().exec("cmd /c java -version");
int ret = p.waitFor();
System.out.println("process terminated with return code: " + ret);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuffer sb = new StringBuffer();
String line;
while((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
System.out.println(sb.toString());

Read/Write to command-line .exe Java

I'm trying to launch a process in java, read the output, write to the program, then read what it responds with. From all the other answers on SO, this is what I have come up with:
class Main
{
public static void main(String[] args) {
String line = "";
try {
ProcessBuilder pb = new ProcessBuilder("C:\\myProgram.exe");
Process p = pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
output.write("foo");
output.newLine();
output.flush();
while ((line = input.readLine()) != null) {
System.out.println(line);
}
p.destroy();
}
catch (IOException e){
}
}
}
It launches the program, and gives me the output just as expected.
When i write foo, I expect the program to come back with another response, but it never does.
What am I doing wrong?

Using PrintWriter in Java

I trying to run multiple command shells from Java. I am able to do that (and get the output in the console using PrintWriter). However, I want to be able to get the output of each command in a separate String. Is that possible?
Here is a part of the code :
File wd = new File("/bin");
Process proc = null;
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd ..");
out.println("ls");
System.out.println("moving to /var directory");
out.println("cd /var/");
out.println("ls");
//get output of ls command in string variable
out.println("cd ..");
out.println("cd /etc/");
out.println("ls -a");
out.println("ps");
out.println("exit");
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
Have you tried putting a section like
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = in.readLine()) != null) {
builder.append(line).append("\n");
}
String commandOutput = builder.toString();
after each command? Is that roughly what you are trying to achieve?

BufferedReader and process.getOutputStream()

I'm simply trying to execute a process in Java, so
Runtime runtime = Runtime.getRuntime();
this.process = null;
try {
this.process = runtime.exec(new String[] {
properties.getPropertyStr("ffmpegExecutable", "/usr/bin/ffmpeg"),
"-i", this.streamEntry.getSource(),
"-vcodec", "copy",
"-acodec", "copy",
this.streamEntry.getDestination()
});
} catch (IOException e) {
e.printStackTrace();
return;
}
BufferedReader stdout = new BufferedReader(???process.getOutputStream());
I simply want to be able to read the output of the process line by line. How do I do this?
BufferedReader is; // reader for output of process
String line;
// getInputStream gives an Input stream connected to
// the process standard output. Just use it to make
// a BufferedReader to readLine() what the program writes out.
is = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = is.readLine()) != null)
System.out.println(line);
BufferedReader in
= new BufferedReader(new InputStreamReader(process.getInputStream()));

Categories