I want to be able to make run a system command on Mac OSX from within Java. My code looks like this:
public void checkDisks() throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec("df -h");
int exitValue = p.waitFor();
System.out.println("Process exitValue:" + exitValue);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
}
System.out.println(line);
}
This always returns null and an exitValue of 0. Never done this before in Java so any thoughts or suggestions greatly appreciated.
Your code is almost OK, you just misplaced the println
public void checkDisks() throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec("df -h");
int exitValue = p.waitFor();
System.out.println("Process exitValue:" + exitValue);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
System.out.println(line);
}
}
I believe it's what you're trying to achieve.
try this
public void checkDisks() throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec(new String[]{"df","-h"});
int exitValue = p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line=reader.readLine()) != null) {
System.out.println(line);
}
System.out.println("Process exitValue:" + exitValue);
}
Related
I call the.py file in a basic java project and it takes about 30 seconds to run.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
Process proc;
String line ="";
BufferedReader in;
try {
proc = Runtime.getRuntime().exec("D:\\anaconda\\python.exe " +
"D:/2017/Python/pythonProject8/main.py " +
"D:\\2017\\Python\\pythonProject8\\flower1.jpg");
proc.waitFor();
in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
result:
enter image description here
But this code is skipped when I use spring-boot.
#GetMapping("test")
public String test(){
System.out.println(1);
Process proc;
String line = "";
String result = "";
try {
proc = Runtime.getRuntime().exec("D:\\anaconda\\python.exe " +
"D:/2017/Python/pythonProject8/main.py " +
"D:\\2017\\Python\\pythonProject8\\flower3.jpg");// 执行py文件
proc.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((line = in.readLine()) != null) {
System.out.println(line);
result += line;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(2);
return result;
}
result:
enter image description here
I want to know how to run spring-boot properly.
thanks.
If your.py file takes a long time to run then you shouldn't use Process and use ProcessBuilder instead.
public ArrayList<String> getPasswords(String path) throws IOException {
String result = "";
ProcessBuilder processBuilder = new ProcessBuilder("D:\\anaconda\\python.exe ", "D:\\2017\\Python\\pythonProject8\\main.py",path);
//The path here is me.py needs to be passed in
processBuilder.redirectErrorStream(true);
final Process process = processBuilder.start();
final BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s = null;
int i = 0;
while ((s = in.readLine()) != null)//This if is I need to ignore some of the output
{
i++;
if (i >6) {
result += s + '\n';
}
}
if (!result.equals("")){
return new ArrayList<String>(Arrays.asList(result.split("\n")));
}
return new ArrayList<String>();
}
You may run an error "DLL load failed while importing XXXX".
Please update the packages required for python.
I am executing a command which returns me the Revision number of a file; 'fileName'. But if there is some problem executing the command, then the application hangs up. What can I do to avoid that condition? Please find below my code.
String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName;
Process p = Runtime.getRuntime().exec(cmd) ;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
I guess the issue is that you are only reading InputStream and not reading ErrorStream. You also have to take care that both the streams are read in parallel. It may so happen that currently the data piped from the output stream fills up the OS buffer, your exec command will be automatically be suspended to give your reader a chance to empty the buffer. But the program will still be waiting for the output to process. Hence, the hang occurs.
You can create a separate class to handle both the Input and Error Stream as follows,
public class ReadStream implements Runnable {
String name;
InputStream is;
Thread thread;
public ReadStream(String name, InputStream is) {
this.name = name;
this.is = is;
}
public void start () {
thread = new Thread (this);
thread.start ();
}
public void run () {
try {
InputStreamReader isr = new InputStreamReader (is);
BufferedReader br = new BufferedReader (isr);
while (true) {
String s = br.readLine ();
if (s == null) break;
System.out.println ("[" + name + "] " + s);
}
is.close ();
} catch (Exception ex) {
System.out.println ("Problem reading stream " + name + "... :" + ex);
ex.printStackTrace ();
}
}
}
The way you use it is as follows,
String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName;
Process p = Runtime.getRuntime().exec(cmd) ;
s1 = new ReadStream("stdin", p.getInputStream ());
s2 = new ReadStream("stderr", p.getErrorStream ());
s1.start ();
s2.start ();
p.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(p != null)
p.destroy();
}
This code is based on the same idea Arham's answer, but is implemented using a java 8 parallel stream, which makes it a little more concise.
public static String getOutputFromProgram(String program) throws IOException {
Process proc = Runtime.getRuntime().exec(program);
return Stream.of(proc.getErrorStream(), proc.getInputStream()).parallel().map((InputStream isForOutput) -> {
StringBuilder output = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(isForOutput))) {
String line;
while ((line = br.readLine()) != null) {
output.append(line);
output.append("\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return output;
}).collect(Collectors.joining());
}
You can call the method like this
getOutputFromProgram("cmd /C si viewhistory --fields=revision --project="+fileName);
Note that this method will hang if the program you are calling hangs, which will happen if it requires input.
I have to launch console commands from java to publish to verdaccio.
So it works(pretty bad), but for few packages processes stucks. When I destroy them it returns code 137, witch means not enough memory. I watched in profiler, I have much more free heap, then used.
Maybe it's not because not enough memory.
How to understand why it stucks and how to fix it?
protected String execCommand(List<String> command) throws IOException, NpmAlreadyExistException{
ProcessBuilder pb = new ProcessBuilder(command);
File workingFolder = new File(System.getProperty("user.dir"));
pb.directory(workingFolder);
Process process = pb.start();
try {
boolean finished = process.waitFor(60, TimeUnit.SECONDS);
logger.info("PROC FINISHED: " + finished);
if (!finished) {
process.destroyForcibly();
int exitCode = process.waitFor();
logger.info("PROC EXIT CODE: " + exitCode);
return null;
}
} catch (InterruptedException e) {
logger.info("PROCESS WAS INTERRUPTED!!!");
logger.info(e.getMessage());
return null;
}
logger.info("PROC EXIT CODE: " + process.exitValue());
String s;
BufferedReader stdErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder err = new StringBuilder();
while ((s = stdErr.readLine()) != null) {
err.append("\n").append(s);
}
stdErr.close();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder result = new StringBuilder();
while ((s = stdInput.readLine()) != null) {
result.append("\n").append(s);
}
stdInput.close();
process.destroy();
logger.info(String.format("execCommand response [stdin]: %s", result));
logger.info(String.format("execCommand response [stdErr]: %s", err));
if (err.length() != 0) {
if (err.toString().contains("Update the 'version' field in package.json and try again.")) {
throw new NpmAlreadyExistException("Пакет с таким именем и версией уже существует в репозитории.");
}
}
return result.toString();
}
Thank you!
class cmdln_file {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd /c dir");
//Process pr = rt.exec("C://apkfiles//new_pro2.apk");
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();
}
}
}
Your question is not so clear, but if your code runs normally, replace cmd /c dir with cmd /c dir > test.txt
I'm trying to make a java program which will get the serial of the hard disk thru cmd or terminal. I'm trying it on both OS (Windows, Linux). I'm having trouble with Linux, it returns a whitespace, when I type hdparm -I /dev/sda | grep Serial in the terminal It shows the serial number of the hard drive.
Question how can I get or display the serial number.
here's my code:
private static String OS= System.getProperty("os.name").toLowerCase();
private static String system;
private static String serial;
private void getVol(String drive)
{
String query=new String();
if(isWindows())
{
query="cmd /c"+" vol "+drive+":";
}
else if(isUnix())
{
query="hdparm -I /dev/sda | grep Serial";
}
try
{
Runtime rt=Runtime.getRuntime();
InputStream is=rt.exec(query).getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String line;
if(isWindows())
{
br.readLine();
line=br.readLine();
line=line.substring(line.lastIndexOf(" ")+1);
serial=line;
}
else if(isUnix())
{
line=br.readLine();
serial=line;
}
}catch(IOException ex)
{
ex.printStackTrace();
}
}
private static boolean isWindows() {
return (OS.indexOf("win") >= 0);
}
private static boolean isUnix()
{
return (OS.indexOf("nux") >= 0);
}
public static void main(String args[])
{
MainClass f=new MainClass();
f.getVol("C");
System.out.println(serial);
}
It's being a while since this was posted, but #giusc solution didn't work for me, so posting my research for others.
Get Hard Disk Drive Serial Number on Linux:
public static void main(String[] args) throws Exception {
String sc = "/sbin/udevadm info --query=property --name=sda"; // get HDD parameters as non root user
String[] scargs = {"/bin/sh", "-c", sc};
Process p = Runtime.getRuntime().exec(scargs);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
if (line.indexOf("ID_SERIAL_SHORT") != -1) { // look for ID_SERIAL_SHORT or ID_SERIAL
sb.append(line);
}
}
System.out.println("HDD Serial number:" + sb.toString().substring(sb.toString().indexOf("=") + 1));
}
Get Hard Disk Drive Serial Number (Manufacture) Windows:
public static void main(String[] args) {
String sc = "cmd /c" + "wmic diskdrive get serialnumber";
Process p = Runtime.getRuntime().exec(sc);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
System.out.println("HDD Serial number: " + sb.substring(sb.toString().lastIndexOf("r") + 1).trim());
}
Your problem is almost certainly that running hdparm requires root privileges. It can't be run by a normal user.
Additionally, the "serial number" that you're reading on Windows is the volume serial number of the primary file system, not of the hard drive. It can be changed rather easily (e.g, using VolumeID).