简体   繁体   中英

Jar file won't run with double click

I can't get my .jar files to run. I have read many similar threads but still can't get it working. I can get it to run from the cmd line if I run "java -jar jar_name.jar" from the folder that contains the file. I ran the Jarfix program that is supposed to fix the file association, but it still does not work. When I export from Eclipse it tells me

JAR export finished with warnings. See details for additional information....Exported with compile warnings: Shutdown/src/Shutdown.java

From what I've read, this is not a real issue, it just means there are warnings in your program. I don't think there's any problem with the code, but it's a small program so I've included it anyways. Any help is appreciated.

import java.io.IOException;
import java.util.Scanner;

public class Shutdown {

    public static void main(String[] args) throws IOException 
    {
        String os;
        String Win = "Windows";
        String choice;

        os = System.getProperty("os.name");

        if(os.contains(Win))
        {
            System.out.println("Congratulations, you are running "+ os +  ".\nYou now have three options.");
            do
            {
                PrintMenu();
                Scanner keyboard = new Scanner(System.in);
                choice = keyboard.next();
                ReturnMenu(choice);//passes user input as argument to method
            }
            while(choice !="1" || choice != "2" || choice !="3");

        }

        else
            System.out.println("You Are Using A Non-Windows System. Please upgrade.");

    }

    public static void shutdown() throws IOException
    {
        Runtime run = Runtime.getRuntime();
        Process proc = run.exec("shutdown -s -t 0");
    }

    public static void restart() throws IOException
    {
        Runtime run = Runtime.getRuntime();
        Process proc = run.exec("shutdown -r -t 0");
    }

    public static void logoff() throws IOException
    {
        Runtime run = Runtime.getRuntime();
        Process proc = run.exec("shutdown -l");
    }

    public static void PrintMenu()
    {
        System.out.println("Please make a selection:"
                    + "\n1 - Shut down\n2 - Restart\n3 - Log Off\n");

    }

    public static void ReturnMenu(String in) throws IOException, NumberFormatException
    {
        int x;

        try
            {
            x = Integer.parseInt(in);//cast user input to int to be used in switch statement
            }
        catch (NumberFormatException e)//catches non-number input that can't be case to int
            {
            x=4;//caught exception sets x to 4 to cause loop to keep running
            }

        switch (x)
        {
        case 1:
            shutdown();
            break;
        case 2:
            restart();
            break;
        case 3:
            logoff();
            break;
        default:
            System.out.println("Invalid menu selection. Please try again.");
        }

    }
}
 Select your File/Project -- Export -- Runnable JAR -- Select class with main() in 
Launch Configuration -- destination
  • If you want dependencies for your class in the generated JAR, select 1st option (Extract required libraries into generated JAR)
  • If you don't need them but just your code, select 3rd option (Copy required libraries into a sub-folder next to generated JAR)

Hope this helps :)

On Windows, a standard Java installation associates double-clicking of .jar files with the javaw.exe program, not java.exe . The difference is that javaw.exe has no console (command window).

Since you have no console, you have no System.in . I haven't had a chance to try it, but my guess is that attempting to read from System.in will result in immediately reaching the end of the stream, which will cause a Scanner's next* methods to throw a NoSuchElementException.

Im summary, you can't use System.in. If you want to prompt the user for input, use Swing or JavaFX.

Additional note: You must not compare Strings using == or != in Java, as those operators compare object references instead of comparing String values. Use the equals method instead. For instance:

while (!choice.equals("1") && !choice.equals("2") && !choice.equals("3"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM