Problem calling linux program from Java
Hi all,
I'm a little stumped. I hope you can help.
I've just migrated a Java program that does image conversion using ImageMagick's "mogrify" and "convert" from Windows to Linux (Slackware 9.1) and I've come across a problem I'm not sure how to solve. I'm not that experienced with Linux but I figured someone here probably has a quick fix to this problem or can steer me in the right direction.
My Java program, see below, calls mogrify and convert, upon an image being uploaded from a web page. When running my Java program in Linux the image file I target does not get resized. It appears as though it gets touched because the timestamp changes but the file doesn't change in size as expected.
When I run the same command the Java program calls from the Linux shell, mogrify and convert work as expected.
What am I doing incorrectly when calling mogrify or convert from Java within Linux? I've tried changing the file permissions where the image is uploaded but that didn't help.
Here's the system output created upon running the Java program. The "ERROR>" and "OUTPUT>" lines are created by Runtime process error and input streams collected upon calling mogrify and convert.
System out From When Calling Mogrify from Java
spasco@spasco:~/webapps/ROOT/WEB-INF/classes$ java -cp . com.test.ImageMagickTest
resize cmd = mogrify -verbose -debug -type Optimize -resize '350x260>' -quality 75 +profile '*' test.JPG
ERROR>test.JPG=>test.mgk MGK 1024x768 DirectClass 8-bit 101kb 0.1u 0:01
OUTPUT>test.JPG JPEG 1024x768 DirectClass 8-bit 101kb 0.1u 0:01
ExitValue: 0
Java Program
Code:
package com.test;
import com.openforrent.util.FileUtility;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class ImageMagickTest {
public static void main(String[] args) {
resize(".","test.JPG","resize");
}
public static void resize(String dir, String fileName, String resizeType) {
Runtime rt = null;
Process proc = null;
StreamGobbler errorGobbler = null;
StreamGobbler outputGobbler = null;
String cmd = null;
try {
int dotIndex = fileName.indexOf('.');
String newFileName = fileName.substring(0, dotIndex);
if(resizeType.equalsIgnoreCase("thumb110")) {
newFileName = newFileName + "_110.jpg";
cmd = "convert -verbose -debug -type Optimize -resize \'110x110>\' -quality 75 +profile \'*\' " + dir + "/" + fileName + " " + dir + "/" + newFileName;
System.out.println("thumb110 cmd = " + cmd);
}
else if(resizeType.equalsIgnoreCase("thumb200")) {
newFileName = newFileName + "_200.jpg";
cmd = "convert -verbose -debug -type Optimize -resize \'200x200>\' -quality 75 +profile \'*\' " + dir + "/" + fileName + " " + dir + "/" + newFileName;
System.out.println("thumb200 cmd = " + cmd);
}
else if(resizeType.equalsIgnoreCase("thumb350")) {
newFileName = newFileName + "_350.jpg";
cmd = "convert -verbose -debug -type Optimize -resize \'350x260>\' -quality 75 +profile \'*\' " + dir + "/" + fileName + " " + dir + "/" + newFileName;
System.out.println("_350 cmd = " + cmd);
}
else if(resizeType.equalsIgnoreCase("resize")) {
cmd = "mogrify -verbose -debug -type Optimize -resize \'350x260>\' -quality 75 +profile \'*\' test.JPG";
System.out.println("resize cmd = " + cmd);
}
rt = Runtime.getRuntime();
proc = rt.exec(cmd);
// any error message?
errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
}
catch (IOException e) {
e.printStackTrace();
System.out.println("e.getMessage() = " + e.getMessage());
}
catch (InterruptedException e) {
e.printStackTrace();
System.out.println("e.getMessage() = " + e.getMessage());
}
finally {
if(rt != null) {
rt = null;
}
if(proc != null) {
proc = null;
}
if(errorGobbler != null) {
errorGobbler = null;
}
if(outputGobbler != null) {
outputGobbler = null;
}
}
}
public static String createDuplicateImage(String useDir, String fileName) {
FileUtility.copyImageFile(useDir, fileName);
return fileName + "_thumb";
}
}
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(type + ">" + line);
}
catch (IOException ioe) {
System.out.println("ioe.getMessage() = " + ioe.getMessage());
ioe.printStackTrace();
}
}
}
|