Running JAR file on Windows

前端 未结 25 2018
故里飘歌
故里飘歌 2020-11-22 01:57

I have a JAR file named helloworld.jar. In order to run it, I\'m executing the following command in a command-line window:



        
相关标签:
25条回答
  • 2020-11-22 02:22

    If you need to distribute your .jar file and make it runnable at other people's Windows computers, you can make a simple .bat file like this in the command prompt:

    java -jar MyJavaTool.jar
    

    and place the .bat file in the same directory as your .jar file.

    0 讨论(0)
  • 2020-11-22 02:24

    Easiest route is probably upgrading or re-installing the Java Runtime Environment (JRE).

    Or this:

    • Open the Windows Explorer, from the Tools select 'Folder Options...'
    • Click the File Types tab, scroll down and select JAR File type.
    • Press the Advanced button.
    • In the Edit File Type dialog box, select open in Actions box and click Edit...
    • Press the Browse button and navigate to the location the Java interpreter javaw.exe.
    • In the Application used to perform action field, needs to display something similar to C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe" -jar "%1" % (Note: the part starting with 'javaw' must be exactly like that; the other part of the path name can vary depending on which version of Java you're using) then press the OK buttons until all the dialogs are closed.

    Which was stolen from here: http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html

    0 讨论(0)
  • 2020-11-22 02:24

    There is way without requiring user to do changes on his PC. Runtime.getRuntime.exec() allows us to start cmd.exe and execute commands inside of it. So, it's possible for java program to run itself in command prompt when user clicks .jar file.

    public static void main(String[] args) throws IOException {
        if(args.length == 0) {
            Process p = Runtime.getRuntime().exec("cmd.exe /c start java -jar " + (new File(NameOfClass.class.getProtectionDomain().getCodeSource().getLocation().getPath())).getAbsolutePath() + " cmd");
        } else {
            //code to be executed
        }
    }
    
    0 讨论(0)
  • There are many methods for running .jar file on windows. One of them is using the command prompt.

    Steps :

    1. Open command prompt(Run as administrator)
    2. Now write "cd\" command for root directory
    3. Type "java jar filename.jar" Note: you can also use any third party apps like WinRAR, jarfix, etc.
    0 讨论(0)
  • 2020-11-22 02:26

    You want to check a couple of things; if this is your own jar file, make sure you have defined a Main-class in the manifest. Since we know you can run it from the command line, the other thing to do is create a windows shortcut, and modify the properties (you'll have to look around, I don't have a Windows machine to look at) so that the command it executes on open is the java -jar command you mentioned.

    The other thing: if something isn't confused, it should work anyway; check and make sure you have java associated with the .jar extension.

    0 讨论(0)
  • 2020-11-22 02:26

    Create .bat file:

    start javaw -jar %*
    

    And choose app default to open .jar with this .bat file.

    It will close cmd when start your .jar file.

    0 讨论(0)
提交回复
热议问题