-3

I searched long to find code to run a jar file, but this is the only code I found:

public class Main {
    
    public static void main (String args[]) {
        try {
            Runtime.getRuntime().exec("java -jar C:/Users/lordb/Desktop/Server/NewBuildTest/spigot-1.18.1.jar");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

but it didn't work. Just nothing happens. No Exception, no Error, nothing. Can someone help me?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 4
    is that an executable jar, or are you trying to include it as a dependency? And are you trying to do this from the commandline or from inside of your application? And why is this tagged "minecraft"? – Roddy of the Frozen Peas Mar 07 '22 at 19:31
  • its an executable jar file and if i run it normally it works.I try this from inside a application – LordBoettcher Mar 07 '22 at 19:36
  • What do you mean with standart intput/output? – LordBoettcher Mar 07 '22 at 19:52
  • 1
    Difficult to know without knowing what the main class does when you run it normally. You can of course invoke MainClass.main directly if it's in the classpath – g00se Mar 07 '22 at 19:53
  • I added the full code if that helps you – LordBoettcher Mar 07 '22 at 20:00
  • 1
    I suspect this is an [XY Problem](https://xyproblem.info/). What is your actual goal here? – OneCricketeer Mar 07 '22 at 20:05
  • I want to run the jar file at the path in the code. The file is an application with a gui if thats important – LordBoettcher Mar 07 '22 at 20:14
  • I understand what minecraft is. My point is that you should add the JAR to the CLASSPATH, then `import` its main method, and run that. Not shell out to run a JAR. If that is really all you want, use a `.sh` or `.bat` script, not Java – OneCricketeer Mar 10 '22 at 17:36
  • Editing questions stops being acceptable after you have a correct answer for the question as originally asked. **Do not** edit a question in a way that makes any existing answer no longer make sense -- the correct thing to do after the first problem you asked about is solved, if a new/different problem is exposed, is to ask a separate question. – Charles Duffy Mar 10 '22 at 17:49

1 Answers1

0

There is at least one cause, because there is no single command with the name

"java -jar C:/Users/lordb/Desktop/Server/NewBuildTest/spigot-1.18.1.jar"

What you want, is to execte the command "java" with the 2 arguments "-jar" and "C:/Users/lordb/Desktop/Server/NewBuildTest/spigot-1.18.1.jar". You can do that by passing the command and its arguments via String array. In your case that would be an array of 3 Strings.

Javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String[])

In that javadoc you can see that exec returns a new Process object. Such Process provides more information, e.g. the exit code of your process (via waitFor) and access to (error) output messages (via getInputStream).

Javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html

Example: Printing a Java InputStream from a Process

Note: You didn't specify which Java version you are using, so i just picked a "random" version for the javadoc links. You might need to look for the version you are using.

Sheldon
  • 234
  • 1
  • 6
  • Thank you for your help :). But what do i have to do now? I updated the code in the question itself. – LordBoettcher Mar 08 '22 at 15:54
  • 1
    I'm not sure what information i could add. If your java process (java -jar ...) provides any output (eg. error messages), then you can access that output via Process.getInputStream(). In case you don't know how to access an InputStream, this is Java basics, so there are tons of examples available. – Sheldon Mar 08 '22 at 17:26
  • I mean: What do i have to do to run the jar application at this path? – LordBoettcher Mar 08 '22 at 17:36
  • Because the jar application doesnt start – LordBoettcher Mar 08 '22 at 18:26
  • 1
    I think there is no way to answer your question, without getting more information. I recommend to get the output from your Process object and then update your original post. – Sheldon Mar 08 '22 at 18:35
  • This is the only output i can get: java.io.BufferedOutputStream@119d7047 – LordBoettcher Mar 08 '22 at 19:43
  • Or what do you mean with getting the output of the process? – LordBoettcher Mar 08 '22 at 19:43
  • Please update the code in your original post, so we can see which code produces the output "java.io.BufferedOutputStream@119d7047". – Sheldon Mar 08 '22 at 19:52
  • Nvm i'm dumb, I just tested it with this program not with another – LordBoettcher Mar 08 '22 at 19:59
  • 1
    I wonder that the documentation states that calling `exec` with one string will be the same as calling it with an `array` obtained by splitting the given string (and it always worked for me - as long as there is no spaces in the path) – user16320675 Mar 09 '22 at 14:15