LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-21-2004, 03:43 AM   #1
spasco
LQ Newbie
 
Registered: Apr 2004
Location: Los Angeles
Posts: 2

Rep: Reputation: 0
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();
}
}
}
 
Old 04-24-2004, 05:57 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Rename the file from test.JPG to test.jpeg or use explicit '-format' parameter.
 
Old 04-24-2004, 10:23 PM   #3
spasco
LQ Newbie
 
Registered: Apr 2004
Location: Los Angeles
Posts: 2

Original Poster
Rep: Reputation: 0
The problem was with the apostrope characters.


convert -verbose -debug -type Optimize -resize '350x260>' -quality 75 +profile '*' " + dir + "/" + fileName + " " + dir + "/" + newFileName


was changed to


convert -verbose -debug -type Optimize -resize 350x260> -quality 75 +profile * " + dir + "/" + fileName + " " + dir + "/" + newFileName


It now works.
 
Old 04-25-2004, 12:13 AM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
ok. I felt I had some problems when using uppercase jpg extension, but maybe I was just too tired when hassling around with it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie/program for calling BBS's Ltkpr49 Linux - Software 1 06-29-2005 09:45 AM
java, call function in calling object exodist Programming 9 06-13-2004 11:49 PM
calling command line functions in java darthczyz Programming 2 12-09-2003 07:01 AM
2 Questions: java calling system commands? PERL vs Java? randomx Programming 28 11-28-2003 08:24 PM
C program code calling a Linux system command Linh Programming 1 06-05-2003 01:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration