Why can't I run a Java program from another Java program? - java

I am running the following code. I ask the user to enter a Java program. The user's program will compile correctly, and if there is any error it will be pointed out. But when I try to run the program it won't run at all. Instead I get this message:
Program finished with exit code 0.
How do I solve this problem?
import java.io.*;
import java.util.*;
public class myprog1 {
public static void main(String [] args) throws IOException, InterruptedException {
System.out.println("enter the candidate name");
Scanner scan = new Scanner(System.in);
String cname = scan.next();
String cfilename = "candidate.java";
String file1 = "/home/prakasha/IdeaProjects/" + cname;
String file2 = "/home/prakasha/IdeaProjects/";
File file = new File("/home/prakasha/IdeaProjects/" + cname);
file.mkdir();
if (!file.exists()) {
System.out.println("filenotfound");
} else {
System.out.println("the code is stored in his dir :" + cname);
File fobj = new File(file, "/" + cfilename);
FileWriter fw = new FileWriter(fobj);
System.out.println("enter the code");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
fw.append(code);
System.out.println(fobj);
fw.close();
ProcessBuilder pb = new ProcessBuilder
("javac", file1 + "/" + cfilename);
pb.inheritIO();
Process process = pb.start();
process.waitFor();
BufferedReader is =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
// reading the output
while ((line = is.readLine()) != null)
System.out.println(line);
ProcessBuilder pb1 = new ProcessBuilder
("java", file1 + "/" + "candidate");
System.out.println(file1 + "/" + "candidate");
pb.inheritIO();
Process process1 = pb1.start();
BufferedReader reading =
new BufferedReader(new InputStreamReader(process1.getInputStream()));
String line1;
while ((line1 = reading.readLine()) != null)
System.out.println(line1);
}
}
}

The argument to the java command is a class name.
/home/prakasha/IdeaProjects/XXX/candidate is not a class name, since / is not a valid character in a class name.
Two ways to fix the problem:
Specify location using -cp argument.
Change the working directory to the folder where the files are.
Also, since you use inheritIO(), there is no output to copy, so getInputStream() is a null input stream.
I'd suggest using the second fix:
new ProcessBuilder("javac", "candidate.java")
.directory(file)
.inheritIO()
.start()
.waitFor();
new ProcessBuilder("java", "candidate")
.directory(file)
.inheritIO()
.start()
.waitFor();

Related

Java Process Builder - Cannot run a simple program

I have a program called darknet. It's a C-program made from Darknet.
I want to run the darknet program in a folder Darknet that looks like this:
I'm going to run darknet with Java Process Builder, but I get no responce from it when I run this code:
// Arguments
String darknetNamePath = darknet.getValue().getFilePath().replace("Darknet/", "./");
String configurationFlag = configuration.getValue().getFilePath().replace("Darknet/", "");
String weightsFlag = weights.getValue().getFilePath().replace("Darknet/", "");
String imageFlag = "data/cameraSnap.png";
String thresholdFlag = "-thresh " + thresholds.getValue();
// Process builder
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(new File("Darknet")); // We need to stand inside the folder "Darknet"
String commandString = "detect " + configurationFlag + " " + weightsFlag + " " + imageFlag + " " + thresholdFlag;
System.out.println("darknetNamePath = " + darknetNamePath);
System.out.println("commandString = " + commandString);
processBuilder.command(darknetNamePath, commandString);
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("\nExited with error code : " + exitCode);
Here is my output. Why doesn't it work for me?
darknetNamePath = ./darknet
commandString = detect cfg/yolov2-tiny.cfg weights/yolov2-tiny.weights data/cameraSnap.png -thresh 0.8
Exited with error code : 0
But when I call darknet file via terminal, then it works.
./darknet detect cfg/yolov2-tiny.cfg weights/yolov2-tiny.weights data/cameraSnap.png -thresh 0.6
UPDATE 2:
Here is my update.
// Arguments
String darknetNamePath = darknet.getValue().getFile().getAbsolutePath();
String configurationFlag = configuration.getValue().getFilePath().replace("Darknet/", "");
String weightsFlag = weights.getValue().getFilePath().replace("Darknet/", "");
String imageFlag = "data/cameraSnap.png";
String thresholdFlag = "-thresh " + thresholds.getValue();
// Process builder
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(darknetNamePath, "detect", configurationFlag, weightsFlag, imageFlag, thresholdFlag);
Process process = processBuilder.start();
if (process.getInputStream().read() == -1) {
System.out.println(darknetNamePath);
System.out.println("detect");
System.out.println(configurationFlag);
System.out.println(weightsFlag);
System.out.println(imageFlag);
System.out.println(thresholdFlag);
System.out.printf("ERROR!");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("\nExited with error code : " + exitCode);
Output:
/home/dell/Dokument/GitHub/Vaadin-DL4J-YOLO-Camera-Mail-Reporter/Vaadin-DL4J-YOLO-Camera-Mail-Reporter/Darknet/darknet
detect
cfg/yolov2-tiny.cfg
weights/yolov2-tiny.weights
data/cameraSnap.png
-thresh 0.3
ERROR!
Exited with error code : 0
UPDATE 3:
This works:
// Arguments
String darkPath = darknet.getValue().getFilePath().replace("Darknet/", "./"); // We need to call ./darknet, not absolute path
String configurationFlag = configuration.getValue().getFilePath().replace("Darknet/", "");
String weightsFlag = weights.getValue().getFilePath().replace("Darknet/", "");
String imageFlag = "data/camera.png";
String thresValue = String.valueOf(thresholds.getValue());
// Process builder
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.directory(new File("Darknet")); // Important
processBuilder.command(darkPath, "detect", configurationFlag, weightsFlag, imageFlag, "-thresh", thresValue);
processBuilder.redirectErrorStream(true); // Important
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("\nExited with error code : " + exitCode);
Your command must break all arguments into separate pieces - including thresholdFlag. It is a good idea to check if the executable exists. If it does not you should check where it is located or fix your Path variable to ensure that it can be located:
File darkpath = new File(darknetNamePath);
String [] cmd = new String[] { darkpath.getAbsolutePath(), "detect", configurationFlag, weightsFlag, imageFlag, "-thresh", String.valueOf(thresholds.getValue()) };
System.out.println("Path: "+darkpath+ " exists="+darkpath.exists());
System.out.println("exec "+Arrays.toString(cmd));
processBuilder.command(cmd);
It is also worth handling STDERR, the easiest way is to redirect STDERR=>STDOUT before calling processBuilder.start()
processBuilder.redirectErrorStream(true);
If you want Java to launch the executable without prefixing the absolute path it needs to be in one of these directories:
System.out.println("PATH COMPONENTS FOR JAVA LAUNCH:");
Arrays.asList(System.getenv("PATH").split(File.pathSeparator)).forEach(System.out::println);
You are using ProcessBuilder the wrong way. The command method takes a executable and arguments as separate strings, not a path and then another string with the actual command and all its arguments. There is no shell involved to do word splitting on the command, so you pass all your distinct arguments as one argument.
I don't have darknet, so here's a simple command using the Unix echo command:
import java.io.*;
public class ProcessBuilderTest {
public static void main(String[] args) throws Exception {
ProcessBuilder processBuilder = new ProcessBuilder();
String[] command = {"/bin/echo", "hello", "world"};
processBuilder.command(command);
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitCode = process.waitFor();
System.out.println("Exited with error code : " + exitCode);
}
}
When I run this, I get:
robert#saaz:~$ java ProcessBuilderTest.java
hello world
Exited with error code : 0
It's not clear to my why your command doesn't produce an error. If I give a bad command (e.g., a trailing space behind "echo"), I get an exception:
Exception in thread "main" java.io.IOException: Cannot run program "/bin/echo ": error=2, No such file or directory
This may be OS specific. Or maybe you have some other darknet executable that gets picked up.

How to call mkvmerge correctly from within a Java program?

I have a few hundred episodes of an anime for which I would like to merge the subtitle and video files I have into one. I decided to write a short Java program to loop through the files and merge them. The video files are named Bleach1.mkv, the subtitles as Bleach1.srt. To avoid any potential issues with spaces the files are saved at the root of my C drive. The code I have written is below.
According to the documentation for mkvmerge, the appropriate command line call would be "mkvmerge -o remux_Bleach1.mkv Bleach1.mkv Bleach1.srt". I have confirmed and this does in fact work. I am running the java program from a different folder at the moment, so I used absolute paths for each of the files.
The command in my code prints out as being "[C:\Program Files\MKVToolNix\mkvmerge.exe, -o C:\Bleach\remux_Bleach_1.mkv C:\Bleach\Bleach_1.mkv C:\Bleach\Bleach_1.srt]"
The error message I am getting back from mkvmerge is "Error: no destination file name was given."
I don't understand exactly how the arguments are passed when using ProcessBuilder - what am I doing wrong?
import org.apache.commons.io.FilenameUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static String mkvmergePath = "C:\\Program Files\\MKVToolNix\\mkvmerge.exe";
public static void mergeVideoAndSubtitles(final File folder, String videoExtension, String subtitleExtension) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry, videoExtension, subtitleExtension);
// for each mkv file that is found
} else if (FilenameUtils.isExtension(fileEntry.getName(), videoExtension)) {
String parentFolderPath = fileEntry.getParentFile().getPath();
String baseName = FilenameUtils.getBaseName(fileEntry.getName());
String outputFileAbsolutePath = parentFolderPath + "\\remux_" + fileEntry.getName();
String inputVideoAbsolutePath = fileEntry.getAbsolutePath();
String inputSubtitleAbsolutePath = parentFolderPath + "\\" + baseName + "." + subtitleExtension;
String param1 = "-o " + outputFileAbsolutePath + " " + inputVideoAbsolutePath + " " +
inputSubtitleAbsolutePath;
// String param2 = "--default-track \"und\"";
// String param3 = "--language 0:und " + baseName + "." + subtitleExtension + "\"";
// System.out.println(param1);
// System.out.println(param2);
// System.out.println(param3);
BufferedReader br = null;
String line;
try {
List<String> list = new ArrayList<String>();
list.add(mkvmergePath);
list.add(param1);
ProcessBuilder build = new ProcessBuilder(list);
System.out.println(build.command());
Process process = build.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
final File folder = new File("C:\\Bleach");
mergeVideoAndSubtitles(folder, "mkv", "srt");
}
}
I was writing a similar program in Go and ran into the same error.
Apparently the spaces in the arguments (e.g. --language 0:eng) cause this problem. I tried to split those into separate arguments and it seems to work:
public static void main(String[] args) throws IOException {
String mkvmerge = "C:\\Program Files\\MKVToolNix\\mkvmerge.exe";
List<String> command = new ArrayList<String>();
command.add(mkvmerge);
command.add("--ui-language");
command.add("en");
command.add("--output");
command.add("D:\\Dump\\test.mkv");
command.add("--language");
command.add("0:eng");
command.add("--default-track");
command.add("0:yes");
command.add("--language");
command.add("1:eng");
command.add("--default-track");
command.add("1:yes");
command.add("--language");
command.add("2:eng");
command.add("D:\\Dump\\Game of Thrones\\Game.of.Thrones.S01.1080p.WEB-DL.DD5.1.H.264-SA89[rartv]\\Game.of.Thrones.S01E01.Winter.Is.Coming.1080p.WEB-DL.DD5.1.H.264-SA89.mkv");
command.add("--sub-charset");
command.add("0:UTF-8");
command.add("--language");
command.add("0:eng");
command.add("D:\\Dump\\Game of Thrones\\Game.of.Thrones.S01.1080p.WEB-DL.DD5.1.H.264-SA89[rartv]\\Subs\\Game.of.Thrones.S01E01.Winter.Is.Coming.1080p.WEB-DL.DD5.1.H.264-SA89.srt");
command.add("--track-order");
command.add("0:0,0:1,0:2,1:0");
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
For some reason this doesn't apply to spaces in paths.
This works in Go as well.

interacting with linux shell prompt in Java

Executor exec = new DefaultExecutor();
exec.setWorkingDirectory("/var/java/apache-tomcat-7.0.47/webapps/Telegram/tg")
CommandLine cl = new CommandLine("bin/telegram-cli -k tg-server.pub -W -U root");
int exitvalue = exec.execute(cl);
How can I get output of this command:
exec.execute(cl);
and run other related commands on telegram-cli command prompt e.g. contact_list, msg contact "Hello world";
It looks to me as though it would be easier just to use ProcessBuilder instead.
ProcessBuilder pb = new ProcessBuilder("bin/telegram-cli","-k","tg-server.pub","-W","-U","root");
pb.directory(new File("/var/java/apache-tomcat-7.0.47/webapps/Telegram/tg"));
Process p = pb.start();
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(p.getOutputStream()),true)) {
String line = null;
pw.println("contact_list");
while ((line = br.readLine()) != null) {
System.out.println("line = " + line);
}
}
Update
It looks like the writes with PrintWriter need to be flushed. You can do this by calling pw.flush() or adding the second argument true to the PrintWriter constructor. I don't know the commands for telegram-cli, but you need to add one that will produce an output you can use to identify when to quit. Here's an example using /bin/sh.
ProcessBuilder pb = new ProcessBuilder("/bin/sh");
Process p = pb.start();
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(p.getOutputStream()),true)) {
String line = null;
pw.println("ls");
pw.println("pwd");
pw.println("echo quit"); // this gives me output I can test for to break the loop
pw.flush();
while ((line = br.readLine()) != null && !line.contains("quit")) {
System.out.println("line = " + line);
}
}
int retcode = p.waitFor();
System.out.println("process ended with " + retcode);
Produces this output:
line = build
line = build.xml
line = dist
line = manifest.mf
line = nbproject
line = src
line = /home/shackle/NetBeansProjects/JavaApplication20
process ended with 0

Java ProcessBuilder not able to run Python script in Java

null from bfr.readLine()
However, there is no problem if I run the python file directly on terminal by firing:
python C:/Machine_Learning/Text_Analysis/Ontology_based.py
The last line in my Python script is >> print(data)
The result of the following code is:
Running Python starts:
First Line: null
Picked up _JAVA_OPTIONS: -Xmx512M
package text_clustering;
import java.io.*;
public class Similarity {
/**
*
* #param args
*
*/
public static void main(String[] args){
try{
String pythonPath = "C:/Machine_Learning/Text_Analysis/Ontology_based.py";
//String pythonExe = "C:/Users/AppData/Local/Continuum/Anaconda/python.exe";
ProcessBuilder pb = new ProcessBuilder("python", pythonPath);
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
System.out.println("Running Python starts: " + line);
line = bfr.readLine();
System.out.println("First Line: " + line);
while ((line = bfr.readLine()) != null){
System.out.println("Python Output: " + line);
}
}catch(Exception e){System.out.println(e);}
}
}
Usually when executing commands using ProcessBuilder, PATH variable is not taken into consideration. Your python C:/Machine_Learning/Text_Analysis/Ontology_based.py is directly working in your CMD shell because it can locate the python executable using the PATH variable. Please provide the absolute path to python command in your Java code. In below code replace <Absolute Path to Python> with the path to python command and its libraries. Usually it will something like C:\Python27\python in Windows by default
package text_clustering;
import java.io.*;
public class Similarity {
/**
*
* #param args
*
*/
public static void main(String[] args){
try{
String pythonPath = "C:/Machine_Learning/Text_Analysis/Ontology_based.py";
//String pythonExe = "C:/Users/AppData/Local/Continuum/Anaconda/python.exe";
ProcessBuilder pb = new ProcessBuilder(Arrays.asList("<Absolute Path to Python>/python", pythonPath));
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
System.out.println("Running Python starts: " + line);
int exitCode = p.waitFor();
System.out.println("Exit Code : "+exitCode);
line = bfr.readLine();
System.out.println("First Line: " + line);
while ((line = bfr.readLine()) != null){
System.out.println("Python Output: " + line);
}
}catch(Exception e){System.out.println(e);}
}
}
Reading from stdin returns null when the script is killed/dies. Do a Process#waitFor and see what the exitValue is. If it isn't 0 then it's highly probable that your script is dying.
I'd try making it work with a dumb script that only writes a value. Make sure that you print all error information from python.
try {
Process p = Runtime.getRuntime().exec(
"python D://input.py ");
BufferedReader in = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
p.waitFor();
} catch (Exception e) {
}
try {
ProcessBuilder pb = new ProcessBuilder("C:/Python27/python", "D://searchTestJava//input.py");
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(".........start process.........");
String line = "";
while ((line = bfr.readLine()) != null) {
System.out.println("Python Output: " + line);
}
System.out.println("........end process.......");
} catch (Exception e) {
System.out.println(e);
}
I tried this one. This script runs a python file with the argument in Java. It also logs about which line, your program is executing. Hope this Helps.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class Test {
public static void main(String... args) throws IOException {
ProcessBuilder pb =
new ProcessBuilder("python","samples/test/table_cv.py","1.pdf");
pb.redirectErrorStream(true);
Process proc = pb.start();
Reader reader = new InputStreamReader(proc.getInputStream());
BufferedReader bf = new BufferedReader(reader);
String s;
while ((s = bf.readLine()) != null) {
System.out.println(s);
}
}
}

running terminal command using java to clear terminal history

I want to get terminal history
So I did this
Runtime rt = Runtime.getRuntime();
pr = rt.exec("/bin/bash -c \"history -c\"");
pr.waitFor();
rt.exec("/usr/bin/xterm");
but there is problem with pr = rt.exec("/bin/bash -c \"history -c\""); , it's not clearing the previous history nither of xterm nor my normal terminal.
Also when I try to print the history it returns nothing (no errors)
p = Runtime.getRuntime().exec("/bin/bash -c \"history\"");
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
System.out.println("printing");
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
I also tried
String[] commands = new String[]{"/bin/sh","-c", "history -c" ,"xterm"};
try {
Process proc = new ProcessBuilder(commands).start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
} catch (IOException e1) {
e1.printStackTrace();
}
still not clearing history.
You can remove the history file yourself by getting the $HISTFILE environment variable. This will always get the correct history file for different shells. I believe the issue you're having is that the you may be using a different shell or have changed your history file location.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RemoveShellHistory {
public static void main(String[] args) {
RemoveShellHistory obj = new RemoveShellHistory();
final String shellPath = System.getenv("SHELL");
String shell = shellPath.substring(shellPath.lastIndexOf("/")+1, shellPath.length());
final String home = System.getenv("HOME");
String command = "rm -v " + home + "/." + shell + "_history";
String output = obj.executeCommand(command);
System.out.println(output);
}
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
Assuming that your java app runs by the same user possessing the .bash_history file:
To delete.
new File(System.getProperty("user.home"), ".bash_history").delete();
To clean (Handle the checked exception at your will).
FileWriter writer = new FileWriter(
new File(System.getProperty("user.home"), ".bash_history"));
writer.write("");
writer.close();

Categories

Resources