0
 if (args.length == 0&&runningFromIntelliJ()==false) {
            String OS = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
            if (OS.indexOf("win") >= 0) {
                String path = CODE.run.class.getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);
                String decodedPath = null;
                try {
                    decodedPath = URLDecoder.decode(path, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                try {
                    Runtime.getRuntime().exec("cmd"+" /c start java -jar \"" + decodedPath + "\" run");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.exit(0);
            }
        }
}

This Code is Starting a programm in cmd after i double clicked it. the problem is that it only works in windows And I want to run it on my raspberry pi. The problem now is that I habe no Idea how a can start a .jar with args in xterm.

runningFromIntelliJ() is just testing if I am running the programm in IntelliJ and skips that part if I do.

Relliks
  • 5
  • 3
  • 2
    Do you understand the code you have posted here? – Thorbjørn Ravn Andersen Feb 11 '20 at 15:26
  • Does this answer your question? [How do I pass parameters to a jar file at the time of execution?](https://stackoverflow.com/questions/456636/how-do-i-pass-parameters-to-a-jar-file-at-the-time-of-execution) – jhamon Feb 11 '20 at 15:26
  • 1
    your code litteraly says "run only on windows" – jhamon Feb 11 '20 at 15:28
  • yes I understad that code... – Relliks Feb 11 '20 at 15:29
  • jhamon I know because on Linux I obviously cant start a jar with cmd – Relliks Feb 11 '20 at 15:30
  • 1
    so, how is this code relevant to your question? – jhamon Feb 11 '20 at 15:31
  • 1
    Your code will not run in any os that does not contain `win` in its name because of the condition you have: `if (OS.indexOf("win") >= 0) {` – smac89 Feb 11 '20 at 15:32
  • 1
    Also if you want it to be truly crossplatform, then remove the dependence on `cmd` and use `"java -jar \"" + decodedPath + "\" run"` – smac89 Feb 11 '20 at 15:34
  • I know that the code will not run in any os that does not contain win. I just posted that code to show what a mean. I just want the same code but for lunix. – Relliks Feb 11 '20 at 15:40
  • to simplify my question: I just want to know how i can do this: Runtime.getRuntime().exec("cmd"+" /c start java -jar \"" + decodedPath + "\" run"); With xterm instead of cmd. – Relliks Feb 11 '20 at 15:44
  • if you know you cannot use cmd and want to use xterm instead, why don't you try that? Take a look at what parameters xterm needs so that it behaves like a `cmd /c start`. You didn't even try anything. – f1sh Feb 11 '20 at 15:51

2 Answers2

0

It seems like your app will do nothing once there's an argument passed to the jar.

if(args.length==0)

will be false hence the whole if. If you have other code to run with Arguments, try this:

java -jar yourJar.jar "arg 1" arg2@

Otherwise, review your code.

ishimwe
  • 1,216
  • 12
  • 13
0

Here is one way to start a program with xterm.

xterm -hold -e '/bin/bash -c "ls -l"'

In this example, the program is simply the ls command, but it should be self-explanatory how to use it for java; just use the example found in another answer.


In your java code, what that looks like is:

Runtime.getRuntime().exec(String.format("xterm -hold -e '/bin/bash -c \"%s\"'", "java -jar '" + decodedPath + "' run"));

or without a shell:

Runtime.getRuntime().exec(String.format("xterm -hold -e 'java -jar \"%s\" run'", decodedPath));
smac89
  • 39,374
  • 15
  • 132
  • 179
  • Thaks but if I try this It says " xterm: Can't execvp '/bin/bash: Datei oder Verzeichnis nicht gefunden" (File or directory not found in english) – Relliks Feb 11 '20 at 15:57
  • @Relliks, try just `bash` or `sh` – smac89 Feb 11 '20 at 15:58
  • what do you mean? I honnestly have no clue of Linux – Relliks Feb 11 '20 at 16:01
  • @Relliks, swap `/bin/bash` with just `bash` or `sh`. Either of them should work. They are both shells that can be used to run commands – smac89 Feb 11 '20 at 16:02
  • @Relliks I'm surprised that none of those are working. See my edit that shows how to run without specifying a shell – smac89 Feb 11 '20 at 16:21
  • I dont know why, but even the code without specifying a shell is having the same error. I am trying to start the jar on my raspberry pi if this is helpful. – Relliks Feb 11 '20 at 16:52
  • 1
    @Relliks are you sure you have `xterm` and `java` installed in your raspberry pi? Also what os are you running on your pi? Can you include this in your question. Use the command `uname -a` – smac89 Feb 11 '20 at 16:54
  • yes I am. I am using xterm to run an other java bot. And I if a type uame -a in the termial it says: "Linux raspberrypi 4.14.98-v7+ #1200 SMP Tue Feb 12 20:27:48 GMT 2019 armv7l GNU/Linux" – Relliks Feb 11 '20 at 17:08