Running JAR file on Windows

前端 未结 25 2137
故里飘歌
故里飘歌 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:39

    PreScript: If your prompt appears and disappears immediately, the reason it does so is that your program gets executed and auto shut. Try putting a scanner in the end to terminate and it'll keep your prompt waiting for input before terminating. (Or use delay maybe)

    Was in the very same situation, where running .jar from cmd was working fine, but double clicking did nothing.

    Solution: Open any text editor and write the command line: java -jar Example.jar Save the file as a .bat file. Run this bat file to get the needed output.

    Taking it one step forward, you can convert this bat file to exe file using a simple GUI tool like Bat To Exe Converter.

    Now you can share your .jar as a distribution in .exe file which anyone can use just make sure you keep all the files together. (Especially the .jar and .bat file cause .bat is only a cmd prompt)(How it feels logical)

    I am fairly new to development and learning a lot. Please excuse for any mistakes if committed. Suggestions are welcome.

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

    For compiling:

    javac  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart.java
    

    For running:

    java  -cp ".;./mysql-connector-java-5.0.8.jar;mybatis-3.0.1.jar;ibatis-2.3.0.677.jar" MainStart
    
    0 讨论(0)
  • 2020-11-22 02:44

    If you need to run the jar file by double clicking on it, you have to create it as a "Runnable JAR". you can do it simply with your IDE.

    If you're using eclipse, follow these steps :

        To create a new runnable JAR file in the workbench:
    
    1.From the menu bar's File menu, select Export.
    2.Expand the Java node and select Runnable JAR file. Click Next.
    3.In the  Opens the Runnable JAR export wizard Runnable JAR File Specification page, select a 'Java Application' launch configuration to use to create a runnable JAR.
    4.In the Export destination field, either type or click Browse to select a location for the JAR file.
    5.Select an appropriate library handling strategy.
    Optionally, you can also create an ANT script to quickly regenerate a previously created runnable JAR file.
    

    more information can be found on Eclipse help Page: LINK

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

    In Windows XP * you need just 2 shell commands:

       C:\>ftype myjarfile="C:\JRE1.6\bin\javaw.exe" -jar "%1" %* 
       C:\>assoc .jar=myjarfile  
    

    obviously using the correct path for the JRE and any name you want instead of myjarfile.

    To just check the current settings:

       C:\>assoc .jar  
       C:\>ftype jarfile  
    

    this time using the value returned by the first command, if any, instead of jarfile.

    * not tested with Windows 7

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

    Actually, I faced this problem too, I got around with it by making a .bat runner for my jar file

    here is the code:

    class FileHandler{
       public static File create_CMD_Rnner(){
          int exitCode = -1625348952;
          try{
               File runner = new File(Main.batName);
               PrintWriter printer = new PrintWriter(runner);
               printer.println("@echo off");
               printer.println("title " + Main.applicationTitle);
               printer.println("java -jar " + Main.jarName + " " + Main.startCode );
               printer.println("PAUSE");
               printer.flush();
               printer.close();
               return runner;
           }catch(Exception e){
               System.err.println("Coudln't create a runner bat \n exit code: " + exitCode);
               System.exit(exitCode);
               return null;
           }
       }
    }
    



    Then in Your Main application class do this:

    public class Main{
        static String jarName = "application.jar";
        static String applicationTitle = "java Application";
        static String startCode = "javaIsTheBest";
        static String batName = "_.bat";
    
    
        public static void main(String args[]) throws Exception{
            if(args.length == 0 || !args[0].equals(startCode)) {
                Desktop.getDesktop().open(FilesHandler.create_CMD_Rnner());
                System.exit(0);
            }else{
                //just in case you wanted to hide the bat
                deleteRunner();
                // Congratulations now you are running in a cmd window ... do whatever you want
                //......
                System.out.println("i Am Running in CMD");
                //......
                Thread.sleep(84600);
            }
        }
    
    
        public static void deleteRunner(){
            File batRunner = new File(batName);
            if(batRunner.exists()) batRunner.delete();
        }
    }
    


    Please Note that

    1. this code (my code) works only with a jar file, not a class file.

    2. the jar file must have the same name as the String "jarName" is the Main class

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

    If you have a jar file called Example.jar, follow these rules:

    1. Open a notepad.exe
    2. Write : java -jar Example.jar
    3. Save it with the extension .bat
    4. Copy it to the directory which has the .jar file
    5. Double click it to run your .jar file
    0 讨论(0)
提交回复
热议问题