views:

410

answers:

6

I'm writing a Java Swing Application running on Red Hat Enterprise Linux 5 server that I would like to launch jEdit to view log files.

Here is some example code.

public static void main(String[] args) throws IOException, InterruptedException {

    String cmd = "sh -c \"java -jar /tmp/jEdit/jedit.jar /tmp/test.txt\"";

    System.out.println(cmd);

    Runtime.getRuntime().exec(cmd);

}

The output is:

sh -c "java -jar /tmp/jEdit/jedit.jar /tmp/test.txt"

If I copy and paste the cmd output in a terminal window, it runs fine.

I have tried a bunch of cmd values, but I can never get the jEdit window to be visible.

With changes, this process works fine on Windows.

Is what I'm doing possible on Linux?

Thanks in advance!

+1  A: 

As jEdit is implemented in Java, perhaps it would be easier to check the source for what the main method (in the class declared in the manifest file included in the jedit.jar) does and do the same thing without using Runtime.getRuntime().exec() at all.

If you do want to stick with it, you could try passing the individual commands as an array to exec(), this often solved such problems for me.

Fabian Steeg
I tried passing the command as an array, but it didn't work.
Angelo Marcotullio
+1  A: 

Linux uses the concept of display ports for its X-Windows system. This allows it to maintain a different desktop environment for each user. It also allows a user on remote machine to run a desktop app from the first machine but see the UI on the remote.

Windows, having only one available desktop environment at a time, does not.

First thing you definitely have to do is add the environment variable "DISPLAY=localhost:0" to the environment from which you are launching this. However, you may also need to run 'xhost +localhost' or this may not be allowed.

Double-check, too, that you didn't successfully launch a bunch of jEdit processes that are now zombies (using top) and kill them if necessary (using kill).

Benjamin Cox
If I can run xclock, does that seem to indicate that the display is setup correctly?
Angelo Marcotullio
+1  A: 

Runtime.exec() needs some special attention. The exec method that accepts a String uses the space character as a delimiter to break up the string into commands. You need to use the exec method that accepts a String[]. Read more here, specifically near the bottom.

Nemi
I've tried that:String[] cmd1 = {"/bin/sh", "-c", "java -jar", "/tmp/jEdit/jedit.jar", "/tmp/test.txt"};Process p1 = Runtime.getRuntime().exec(cmd1);// this does not open the jEdit WindowString[] cmd2 = {"/bin/sh", "-c", "java -jar", "/tmp/helloWorld.jar"};Process p2 = Runtime.getRuntime().exec(cmd2);// This prints Hello World to the screen.I really think the issue is with the Display.
Angelo Marcotullio
Did you try String[] cmd1 = {"/bin/sh", "-c", "java -jar /tmp/jEdit/jedit.jar"}?
Nemi
A: 

I´ve done this once and I got the same problem

What I've done is to write the command line into a text file and then execute the text file as a shell script file. It worked fine for me.

marionmaiden
A: 

Hi i using Exec() command in swing program but i want output in GUI concept not in a Command Prompt .....in this i'm using arrays to set a path for an example i'm going to fetch program files information in the directory

"c:/ProgramFiles/dir"

in this i want an output in same console application but dont in seperate Command prompt

Arunkumar
A: 

thanks Marionmaiden Can you give your coding

Arunkumar