I have a problem where I can successfully call the jar file from the terminal but I can't call it successfully from another Java file. As you can see it works properly from the terminal.
However, it returns null when called from a class file despite feeding in the same arguments.
The relevant bit of code would appear to call the jar file the same way.
try{
String output = "java -jar Translator.jar " + word;
Process p = Runtime.getRuntime().exec(output);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = in.readLine();
System.out.println(line);
while ((line = in.readLine())!= null) {
line = in.readLine();
line = line.trim();
System.out.println(line);
}
}
Related
I am trying to write a Java application that executes a python script in order to return a value to the original program. However, the script is written in Python3 so I can't use Jython.
My current program works fine in Intellij but when I export to a JAR file it no longer works (I'm assuming because the filepath is different). I know questions similar to this have been asked in the past but none of the solutions seem to be working.
String currentPath = new File("").getAbsolutePath();
String path = Paths.get(currentPath, "src", "com", "engine", "pythonScript.py").toUri().toString();
String[] cmd = new String[]{"python", path, data};
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String output;
while ((output = stdInput.readLine()) != null) {
System.out.println(output);
if (checkOutput(output)) {
return output;
}
}
BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s;
while ((s=error.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
I want to execute a python script from java.
The code is getting in to the python file but only executes first line of the file.
following is the code:
Process p = Runtime.getRuntime().exec("python "+dir+"/pyfiles/testfile.py");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
value = in.readLine();
after the first line nothing is executed.
what is the solution?
'dir' value is getting from
final String dir = System.getProperty("user.dir");
link to python file:
https://drive.google.com/file/d/1tvkFTM_Oo5gTS7FyzeNgoeY5DLitFQjD/view?usp=sharing
The problem seems, that you are only reading the first line of your BufferedReader. So change your code as follows:
Process p = Runtime.getRuntime().exec("python "+dir+"/pyfiles/testfile.py");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
it worked fine when I passed like this:
String cmd = "python2.7 "+dir+"/pyfiles/getGitFiles.py "+ownerVal+" "+repoVal+" "+folderVal+" "+branchVal+" "+Values.accessToken;
System.out.println(cmd);
Process p = Runtime.getRuntime().exec(cmd);
passing the arguments inside the 'exec' itself is causing the problem.
I am working with eclipse and I try to read a HTML file to display it in a web browser (I am doing a http Java server).
I was using FileReader() function and it worked fine, then I restarted eclipse and it gave me the following error:
Exception in thread "main" java.io.FileNotFoundException: index.html
this is how I read the file :
String httpContent;
BufferedReader br = new BufferedReader(new FileReader("index.html"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
httpContent = sb.toString();
} finally {
br.close();
}
the file "index.html" is on the same level as the java file.
What should I change to get FileReader to work again?
So I don't know if closing eclips resets some parameters but to solve my problem, I just put the path to my HTML file from the project folder
so : src/Ex5/index.html instead of index.html
While executing the following code it gives a file not found error at a:28 (in comments). Is it because of directory is not refreshed, or the file not created by subprocess before executing line at a:28?
File outputFile = new File("RunstatsCmd.db2");
FileWriter out = new FileWriter(outputFile);
String cmd = "DB2CMD;DB2;"+" EXPORT TO "+ "\"C:\\file.xml\"" +" OF IXF MESSAGES "+"\"C:\\msg.txt\""+" SELECT * FROM OLTP.ACCOUNT_DETAILS";
out.write("CONNECT TO OLTPDB;\n");
out.write(cmd + ";\n");
out.write("CONNECT RESET;\n");
out.close();
System.out.println("before creating connection....");
Class.forName ("com.ibm.db2.jcc.DB2Driver").newInstance ();
Process p = Runtime.getRuntime().exec("db2 -vtf RunstatsCmd.db2");
// open streams for the process's input and error
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
String s;
// read the output from the command and set the output variable with
// the value
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command and set the error
// variable with the value
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
// destroy the process created
// delete the temporary file created
outputFile.deleteOnExit();
System.out.println("query executed...");
p.waitFor();
//a:28////////////////////////
FileInputStream fis=new FileInputStream("C:\\file.xml");
int i;
while((i=fis.read())!=-1){
System.out.println((char)i);
}
}catch(Exception e){
e.printStackTrace();
}
Backslashes in strings need to be escaped.
"C:\\file.xml"
Alternatively, use forward slashes (they're accepted in Java, even on Windows machines).
"C:/file.xml"
Please use \ character carefully when using for file path.
Change your code to this:
FileInputStream fis=new FileInputStream("C:\\file.xml");
\ is a escape character.
Alternatively you can use:
FileInputStream fis=new FileInputStream("C:/file.xml");
In a java String, \ is an escape character, and so it must be escaped if you want to use it literally.
So to represent C:\file.xml, you'll need to use this java String: "C:\\file.xml".
But note that in java you can always use forward slashes as path separators - even in windows - so this should also work:
FileInputStream fis = new FileInputStream("C:/file.xml");
I'm trying to run an external program in java like this:
Process p = Runtime.getRuntime().exec("./shufflet 1 2 <in.seq> out.seq");
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
while ((line = bri.readLine()) != null) {
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null) {
System.out.println(line);
}
bre.close();
p.waitFor();
Basically, this program that I'm executing (Shufflet) reads in whatever is in in.seq and then writes something to out.seq based on that.
If I copy+paste that line (./shufflet 1 2 <in.seq> out.seq) to the command line it works fine.
If I execute the java program it outputs Usage: shufflet [OPTIONS] NSEQ ORDER <INFILE >OUTFILE which is the error message that Shufflet gives if the parameters are wrong.
I know the parameters are correct because, again, it works if I copy+paste it to the command line.
Any ideas?
Have you tried with DataInputStream ?
DataInputStream myStream = new DataInputStream(p.getInputStream());
while ((line = myStream.readLine()) != null) {
System.out.println(line);
}