Gimp invocation from Java  
Author Message
monochromec





PostPosted: 2005-5-18 1:00:00 Top

java-programmer, Gimp invocation from Java I have the following problem when I try to invoke a shell script from a
running Java program. When using the script from the command line, all
works well. The script in question is a small batch file that simply
invokes GIMP to execute a pre-defined Scheme program that watermarks
and converts an image file.

When invoked from a JVM instance, GIMP itself starts, reads and reads
the script OK but then stops executing according to a process
monitoring utility.

Here's the Java snippet in question:

import java.io.*;
import java.util.*;
import java.lang.*;


public class Convert {
private static void convert (String [] files, String targetDir) {
int retV = 0;
for (int i = 0; i < files.length; i++) {
File file = new File (files [i]);
String tmp = new String (file.getName ());
String base = new String (tmp.substring (0, tmp.lastIndexOf
(".")));
String f = new String (targetDir + "/" + base);
String fS = new String (f + "s");
f += ".jpg";
fS += ".jpg";
File ff = new File (f);
String pre = "";

if (! file.canRead () || file.lastModified () > ff.lastModified ())
{
final String [] args = {"bash.exe", "-c", "cv.sh " + files [i]
+ " " + f + " " + fS};
try {
Process p = Runtime.getRuntime ().exec (args);
retV = p.waitFor ();
} catch (Exception e) {
System.out.println (e.toString ());
}
} else {
pre = ">";
}
System.out.println (pre + tmp + ":" + retV + "\n");
}
}
public static class FFileFilter implements FilenameFilter {
public boolean accept (File dir, String name) {
return name.toLowerCase ().endsWith (".bmp");
}
}
private static String [] getFiles (String dir) {
File f = new File (dir);

return f.list (new FFileFilter ());
}
public static void main(String[] args) {
String [] files = getFiles (args [0]);

convert (files, "img");
}
}

The shell script in question (cv.sh):


gimp-2.2 -c -i -d -b '(script-fu-process "'$1'" "'$2'" "'$3'")'

I've tried separating the cmd-line arguments for the cv.sh
invocation; the above format is the only version that works. The
environment is the following: Win XP SP2, gimp 2.2.7, Cygwin bash
2.05b.01, Sun jdk 1.5.0_02 as well as Eclipse 3.02 (I've tried both
Java environments to no avail).

Any thoughts?

Cheers, Christoph

 
monochromec





PostPosted: 2005-5-18 1:01:00 Top

java-programmer >> Gimp invocation from Java I have the following problem when I try to invoke a shell script from a
running Java program. When using the script from the command line, all
works well. The script in question is a small batch file that simply
invokes GIMP to execute a pre-defined Scheme program that watermarks
and converts an image file.

When invoked from a JVM instance, GIMP itself starts, reads and reads
the script OK but then stops executing according to a process
monitoring utility.

Here's the Java snippet in question:

import java.io.*;
import java.util.*;
import java.lang.*;


public class Convert {
private static void convert (String [] files, String targetDir) {
int retV = 0;
for (int i = 0; i < files.length; i++) {
File file = new File (files [i]);
String tmp = new String (file.getName ());
String base = new String (tmp.substring (0, tmp.lastIndexOf
(".")));
String f = new String (targetDir + "/" + base);
String fS = new String (f + "s");
f += ".jpg";
fS += ".jpg";
File ff = new File (f);
String pre = "";

if (! file.canRead () || file.lastModified () > ff.lastModified ())
{
final String [] args = {"bash.exe", "-c", "cv.sh " + files [i]
+ " " + f + " " + fS};
try {
Process p = Runtime.getRuntime ().exec (args);
retV = p.waitFor ();
} catch (Exception e) {
System.out.println (e.toString ());
}
} else {
pre = ">";
}
System.out.println (pre + tmp + ":" + retV + "\n");
}
}
public static class FFileFilter implements FilenameFilter {
public boolean accept (File dir, String name) {
return name.toLowerCase ().endsWith (".bmp");
}
}
private static String [] getFiles (String dir) {
File f = new File (dir);

return f.list (new FFileFilter ());
}
public static void main(String[] args) {
String [] files = getFiles (args [0]);

convert (files, "img");
}
}

The shell script in question (cv.sh):


gimp-2.2 -c -i -d -b '(script-fu-process "'$1'" "'$2'" "'$3'")'

I've tried separating the cmd-line arguments for the cv.sh
invocation; the above format is the only version that works. The
environment is the following: Win XP SP2, gimp 2.2.7, Cygwin bash
2.05b.01, Sun jdk 1.5.0_02 as well as Eclipse 3.02 (I've tried both
Java environments to no avail).

Any thoughts?

Cheers, Christoph

 
monochromec





PostPosted: 2005-5-18 1:01:00 Top

java-programmer >> Gimp invocation from Java I have the following problem when I try to invoke a shell script from a
running Java program. When using the script from the command line, all
works well. The script in question is a small batch file that simply
invokes GIMP to execute a pre-defined Scheme program that watermarks
and converts an image file.

When invoked from a JVM instance, GIMP itself starts, reads and reads
the script OK but then stops executing according to a process
monitoring utility.

Here's the Java snippet in question:

import java.io.*;
import java.util.*;
import java.lang.*;


public class Convert {
private static void convert (String [] files, String targetDir) {
int retV = 0;
for (int i = 0; i < files.length; i++) {
File file = new File (files [i]);
String tmp = new String (file.getName ());
String base = new String (tmp.substring (0, tmp.lastIndexOf
(".")));
String f = new String (targetDir + "/" + base);
String fS = new String (f + "s");
f += ".jpg";
fS += ".jpg";
File ff = new File (f);
String pre = "";

if (! file.canRead () || file.lastModified () > ff.lastModified ())
{
final String [] args = {"bash.exe", "-c", "cv.sh " + files [i]
+ " " + f + " " + fS};
try {
Process p = Runtime.getRuntime ().exec (args);
retV = p.waitFor ();
} catch (Exception e) {
System.out.println (e.toString ());
}
} else {
pre = ">";
}
System.out.println (pre + tmp + ":" + retV + "\n");
}
}
public static class FFileFilter implements FilenameFilter {
public boolean accept (File dir, String name) {
return name.toLowerCase ().endsWith (".bmp");
}
}
private static String [] getFiles (String dir) {
File f = new File (dir);

return f.list (new FFileFilter ());
}
public static void main(String[] args) {
String [] files = getFiles (args [0]);

convert (files, "img");
}
}

The shell script in question (cv.sh):


gimp-2.2 -c -i -d -b '(script-fu-process "'$1'" "'$2'" "'$3'")'

I've tried separating the cmd-line arguments for the cv.sh
invocation; the above format is the only version that works. The
environment is the following: Win XP SP2, gimp 2.2.7, Cygwin bash
2.05b.01, Sun jdk 1.5.0_02 as well as Eclipse 3.02 (I've tried both
Java environments to no avail).

Any thoughts?

Cheers, Christoph

 
 
Gordon Beaton





PostPosted: 2005-5-18 2:21:00 Top

java-programmer >> Gimp invocation from Java On 17 May 2005 10:00:33 -0700, email***@***.com wrote:
> When invoked from a JVM instance, GIMP itself starts, reads and
> reads the script OK but then stops executing according to a process
> monitoring utility.

Followup set to c.l.j.help.

Please choose ONE relevant group to post your question to, not three
or more. And when you absolutely must post to multiple groups, don't
multipost. Crosspost instead.

The process is waiting for you to read from its output and error
streams, which you neglect to do in your code. Alternatively, redirect
the output of the child so it doesn't fill the output streams with
data that nobody is reading.

> final String [] args =
> {"bash.exe", "-c", "cv.sh " + files [i]
> + " " + f + " " + fS};

Tip: you don't need to explicitly run a shell in order to run the
script, if the script starts with the correct #! line and is marked
executable. At least that's the case on Unix, I don't know about
Cygwin.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
 
Roland





PostPosted: 2005-5-18 21:32:00 Top

java-programmer >> Gimp invocation from Java On 17-5-2005 19:00, email***@***.com wrote:

> I have the following problem when I try to invoke a shell script from a
> running Java program. When using the script from the command line, all
> works well. The script in question is a small batch file that simply
> invokes GIMP to execute a pre-defined Scheme program that watermarks
> and converts an image file.
>
> When invoked from a JVM instance, GIMP itself starts, reads and reads
> the script OK but then stops executing according to a process
> monitoring utility.
>
> Here's the Java snippet in question:
>
> import java.io.*;
> import java.util.*;
> import java.lang.*;
>
>
> public class Convert {
> private static void convert (String [] files, String targetDir) {
> int retV = 0;
> for (int i = 0; i < files.length; i++) {
> File file = new File (files [i]);
> String tmp = new String (file.getName ());
> String base = new String (tmp.substring (0, tmp.lastIndexOf
> (".")));
> String f = new String (targetDir + "/" + base);
> String fS = new String (f + "s");
> f += ".jpg";
> fS += ".jpg";
> File ff = new File (f);
> String pre = "";
>
> if (! file.canRead () || file.lastModified () > ff.lastModified ())
> {
> final String [] args = {"bash.exe", "-c", "cv.sh " + files [i]
> + " " + f + " " + fS};
> try {
> Process p = Runtime.getRuntime ().exec (args);
> retV = p.waitFor ();
> } catch (Exception e) {
> System.out.println (e.toString ());
> }
> } else {
> pre = ">";
> }
> System.out.println (pre + tmp + ":" + retV + "\n");
> }
> }
> public static class FFileFilter implements FilenameFilter {
> public boolean accept (File dir, String name) {
> return name.toLowerCase ().endsWith (".bmp");
> }
> }
> private static String [] getFiles (String dir) {
> File f = new File (dir);
>
> return f.list (new FFileFilter ());
> }
> public static void main(String[] args) {
> String [] files = getFiles (args [0]);
>
> convert (files, "img");
> }
> }
>
> The shell script in question (cv.sh):
>
>
> gimp-2.2 -c -i -d -b '(script-fu-process "'$1'" "'$2'" "'$3'")'
>
> I've tried separating the cmd-line arguments for the cv.sh
> invocation; the above format is the only version that works. The
> environment is the following: Win XP SP2, gimp 2.2.7, Cygwin bash
> 2.05b.01, Sun jdk 1.5.0_02 as well as Eclipse 3.02 (I've tried both
> Java environments to no avail).
>
> Any thoughts?
>
> Cheers, Christoph
>
Which version of Gimp are you using: a Cygwin version, or a Win32 version.

In case of a Cygwin version, it expects Unix style paths, i.e. with
forward slash / and a path that matches Cygwin mount points, for example
/cygdrive/c/Documents and Settings/YourName/TEMP/img.jpg

The Java executable on your system supplies Windows style paths to the
cv.sh shell script, which then would mismatch with what gimp expects.
To debug, try and print the arguments in the cv.sh shell script, e.g.
echo $1 > /tmp/args.cv.sh.txt
echo $2 >> /tmp/args.cv.sh.txt
echo $3 >> /tmp/args.cv.sh.txt
gimp-2.2 -c -i -d -b '(script-fu-process "'$1'" "'$2'" "'$3'")'

If necessary, convert Windows path to Unix path using cygpath, e.g.
f1=`cygpath -u "$1"`
f2=`cygpath -u "$2"`
f3=`cygpath -u "$3"`
gimp-2.2 -c -i -d -b '(script-fu-process "'$f1'" "'$f2'" "'$f3'")'

[cygpath -w would do the opposite, i.e. from Unix to Windows path.]
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
 
monochromec





PostPosted: 2005-5-24 4:00:00 Top

java-programmer >> Gimp invocation from Java Thanks for the input!

Trouble is, however, this doesn't solve the problem as Gimp doesn't
expect any input or output when run in batch mode. Alas, the
bash script under cygwin works no problems.

Cheers, Christoph


Gordon Beaton schrieb:
> On 17 May 2005 10:00:33 -0700, email***@***.com wrote:
> > When invoked from a JVM instance, GIMP itself starts, reads and
> > reads the script OK but then stops executing according to a process
> > monitoring utility.
>
> Followup set to c.l.j.help.
>
> Please choose ONE relevant group to post your question to, not three
> or more. And when you absolutely must post to multiple groups, don't
> multipost. Crosspost instead.
>
> The process is waiting for you to read from its output and error
> streams, which you neglect to do in your code. Alternatively,
redirect
> the output of the child so it doesn't fill the output streams with
> data that nobody is reading.
>
> > final String [] args =
> > {"bash.exe", "-c", "cv.sh " + files [i]
> > + " " + f + " " + fS};
>
> Tip: you don't need to explicitly run a shell in order to run the
> script, if the script starts with the correct #! line and is marked
> executable. At least that's the case on Unix, I don't know about
> Cygwin.
>
> /gordon
>
> --
> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e

 
 
monochromec





PostPosted: 2005-5-24 4:05:00 Top

java-programmer >> Gimp invocation from Java I'm using a Win32 version of gimp. Trouble is, that the whole script is
working
from a native bash script no problem, only when invoked from a JVM it
starts
giving problems. I checked the cmd-line args with a process debugger;
they appear to
be correct. I'm pretty much at my whit's ends on this one - maybe
somebody out
there can shed more light on this?

Cheers, Christoph