GhostScript: gswin32c hangs, but gswin32 works - java

We are busy converting PDF files to TIFF files using GhostScript 9.06 using the following command:
gswin32c -dNOPAUSE -sDEVICE=tiff24nc -r300 -sCompression=lzw -sOutputFile="C:/destination.tif" "C:/source.pdf" -c quit
This is executed via Java on a Windows server that runs most of our batch tools.
This works great for a large part of our files, but for some files, the process just hangs and the task manager shows that the gswin32c.exe process is using 0% of the CPU. We have already resorted to killing the process after a minute and convert the PDF using PDFBox instead if GhostScript fails to respond.
When using the same command, but with the gswin32 tool, the conversion works perfectly, minus the fact that it opens and closes a GUI window each time the command is executed. Because of this, using gswin32 is not an option because people are working on the server constantly.

Instead of '-c quit' add -dBATCH to the command line. Unless your PDF files are all single pages, you probably want to add a '%d' to the output filename too.

This problem has nothing to do with Ghostscript. You will get the problem with every programm that you run with ProcessBuilder which sends output to Standard out. As Windows buffer only limited amount of text, when you do not read the output in your java programm the called process will hang. So you can run gswin32c successfully when your conversion to pdf will only produce a small amount of status messages. But when you convert a file with many pages the process will hang. The solution is to read the output of the called process in your Java program.
ProcessBuilder processBuilder = new ProcessBuilder(
"C:\\Program Files (x86)\\gs\\gs9.10\\bin\\gswin32c.exe", "-sDEVICE=\"pdfwrite\"",
"-dNOPAUSE", "-dBATCH", "-dSAFER", "-dQUIET", "-sOUTPUTFILE=\"" + fileName + ".pdf\"", "\""
+ fileName + ".ps\"");
processBuilder.redirectErrorStream(true); //Redirect Error Stream to Standard Inputstream so that we have to read only Standard in
Process process = processBuilder.start();
InputStream is = process.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
process.waitFor();

Related

Compiling python program using java file and reading input for python program from the file

I've stored my python code in file and then input is passed through input.txt.
String rollno="13F127";
String file="add";
Process p = Runtime.getRuntime().exec("C:\\Python34\\python C:\\Users\\Raga\\Documents\\"+rollno+"\\"+file+".py < C:\\Users\\Raga\\Documents\\"+rollno+"\\input.txt");
When I run it using jsp file, it takes long time to load and output didnt come. Please help me with this.
I've read this process output using buffered and input reader.
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
Please help me with this!
Do not use redirections (<, >, or |) with exec!
The redirections are read and translated by the interactive shells (here cmd.exe) where they read a command from their standard input. The shell then opens the relevant files and calls the program with redirected standard streams.
exec just does this last part, and passes the < ...input.text as two arguments to the Python program... that do not processes them and also passes them to the script that do not process them either! So the child tries to read on standard input, and keeps waiting here.
So you should:
use ProcessBuilder which according to Runtime javadoc is now the preferred way to start a process with a modified environment
redirect input stream for the subprocess to the file
More or less:
ProcessBuilder pb = new ProcessBuilder("C:\\Python34\\python",
"C:\\Users\\Raga\\Documents\\"+rollno+"\\"+file+".py");
pb.redirectInput(Redirect.fromFile("C:\\Users\\Raga\\Documents\\"+rollno+"\\input.txt"));
Process p = pb.start();

ProcessBuilder running processes in foreground

I want to run an executable written in C++ and to see the cmd promt associated with it in foreground, since the executable prints some lines in the cmd.
I have written the following code, but all processes are created and run in background (In this code I open the dummy cmd.exe process, not my process).
Process p = new ProcessBuilder("C:\\Windows\\System32\\cmd.exe").start();
How can i enable foreground running of processes?
Thanks!
The issue is not whether the process is in the foreground or background. When you start a process using Java, you have to use Java to control that process' lifecyle. The Java API provides you access to various attributes of the process. What you're interested in here is the output of the process. That is represented by the process' InputStream. It seems counterintuitive, but it makes sense because from the perspective of your Java program, the process' output is the program's input. Conversely, if you need to send data to the process, you write to the process' OutputStream.
To sum up, access the process' InputStream and print that out to the command-line:
Process process = new ProcessBuilder("C:\\Path\\To\\My\\Application.exe").start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line);
}
System.out.println(line);
This code, of course, assumes that your process is not waiting for any input, i.e., it is not interactive.
Vivin Paliath's answer is really the way to go, then you can do whatever you want with the output, display it in your own dialogue, log it, interpret it, check for errors or whatever.
But just in case you really want that command window showing up. Execute cmd.exe and get the process' OutputStream and write the command (application.exe) to it ending with a new line.
Something along the lines of:
Process p = new ProcessBuilder("C:\\Windows\\System32\\cmd.exe").start();
out = p.getOutputStream();
out.write("path\\application.exe\r\n".getBytes());
out.flush();
Should usually drain the input stream too though anyway.

Java: OutputStream to .exe Console App issue

I am trying to interact with a simple .exe I created from some Python code. I have tested the .exe through Windows cmd and it works just fine. When I try to send the same input to the .exe through my java program to produce the graphs I need, OutputStream just writes "error" to the console. I have tried sending a string and an integer through OutputStream but the same results are obtained no matter what. I have already interacted with X-Foil.exe, a console application use to produce airfoil data files, with great success through this same java app. As I have to preform curve fitting to the data, I used Python with the matplotlib plugin then used py2exe to create the .exe. I am trying to create a web application with the end goal of designing an aircraft wing, hence the use of java. Here is the method being use which I am having the problem with:
public void PyGrapher(String NACA_4d) {
try {
ProcessBuilder builder = new ProcessBuilder("PyAirfoilGraphing\\dist\\GraphPolars.exe");
builder.redirectErrorStream(true);
Process pr = builder.start();
OutputStream out = pr.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
double CL_alpha;
out.write((NACA_4d + "\n").getBytes());
System.out.println(in.readLine());
System.out.println(in.readLine());
System.out.println(in.readLine());
System.out.println(in.readLine());
//CL_alpha = Double.parseDouble(in.readLine());
pr.waitFor();
pr.destroy();
out.close();
in.close();
} catch (IOException | InterruptedException ex) {
}
}
Here is what I have read in from the console:
Input NACA 4-digit code: error
Traceback (most recent call last):
File "GraphPolars.py", line 16, in <module>
IOError: [Errno 2] No such file or directory: '..\\..\\AirfoilPolars\\NACA_0024.dat'
I am stumped, and have been for quite some time. There is not a problem with the python file, and it runs fine by itself in the current directory. Can anyone help, please?
-Nick K
Could it be something with relative paths? Python default path may not be picking up the local directory/file.
Did you try passing it the full windows path, 'C:\Foo\bar\AirFoil\... ' with properly escaped '\' characters?

Can't launch external program from Java without closing java app

I'm trying to launch an external program from my java swing app using this:
Process proc = Runtime.getRuntime().exec(cmd);
But the external program never actually gets launched until I close out of my java app...everytime.
It waits to launch only after I have closed out.
the external program I am trying to run is an exe that takes arguments so:
cmd = "externalProgram.exe -v --fullscreen --nowing";
What could possibly be wrong here.
Funny enough it works as expected if i try something simple like:
Process proc = Runtime.getRuntime().exec("notepad.exe");
You may need to read from the process's standard output, or close the standard input, before it will proceed. For reading the output, the problem is that the buffer can get full, blocking the program; for closing the input, the problem is that some programs will try to read data from there if it's available, waiting to do so. One or both of these tricks is very likely to straighten things out for you.
You may also read the error output stream to check it the program is actually being unsuccessfully executed
String cmd = "svn.exe";
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = null;
while((line=reader.readLine())!=null){
System.out.println(line);
}
reader.close();
My console shows
Type 'svn help' for usage.
Which evidently shows the program was executed by Java.

Unix Script not working in Java Process Runtime.exec()

I am developing an application in Spring Web MVC where i need to execute some of the linux script..
I am using tomcat version 5.5 for running my project in linux..
My code is looking like this :
Process proc = runtime.exec("sudo cp /var/tmp/mailserverfiles/editinterface.txt /etc/sysconfig/network-scripts/editinterface.txt");
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String line;
while ((line = bufferedreader.readLine()) != null) {
System.out.println("\nOUTPUT = " + line);
}
System.out.print("\nbefore execute6");
try {
if (proc.waitFor() != 0) {
System.err.println("\nexit value = " + proc.exitValue());
}
} catch (InterruptedException e) {
System.err.println("\nERROR = " + e);
}
Here i want to cp a particular file from one location to another using linux script..
But when i am executing this part, i am getting
exit value = 1
as a output.. I have also tried to put this script into .sh file and try to execute that shell script here from Java Code, but i am getting same result..
Can anybody tell me, what should be the reason for this ?
Thanks in advance..
I would guess that sudo is expecting an interactive terminal in order to ask for a password. Since there is no interactive terminal, it prints an error message to stderr and exits with an exit code of 1. You are not reading the error stream, so you won't see any message that it might print.
You will definitely want to read the error stream in any case. Doing so now will help you diagnose what is going wrong at this point.
I assume the user that Tomcat is running under has unrestricted access to sudo? And that it's not being prompted for a password?
It is possible that your search path is weird and that "cp" and "sudo" are not found when you try to execute the command.
Here are some things you could try to track down your problem(s):
Try running the "cp" command without "sudo".
Try giving the full pathname of the command(s). This will avoid search path problems.
By default "sudo" logs failed commands using syslog(3). See if you can find traces in the corresponding logfiles.
Assuming you can run your command from a command line, logged in as the tomcat user - try
ProcessBuilder pb = new ProcessBuilder("/usr/bin/sudo", "cp",
"/var/tmp/mailserverfiles/editinterface.txt",
"/etc/sysconfig/network-scripts/editinterface.txt");
pb.redirectErrorStream(true);
Process proc = pb.start();
... rest of code as before
if things still fail, start debugging. strace should be helpful. e.g. run this shell script
from your java application, and figure out where things fail in the /tmp/trace.txt file:
#!/bin/sh
strace -f sudo cp /var/tmp/mailserverfiles/editinterface.txt /etc/sysconfig/network-scripts/editinterface.txt >/tmp/trace.txt 2>&1
Whilst not directly answering your question, the following will help. You need to read stdout and stderr (to capture all process output), and do this concurrently to prevent blocking of the spawned process. See this answer for more info.

Categories

Resources