Jar文件不会双击运行

时间:2015-02-07 23:37:20

标签: java eclipse jar

我无法运行我的.jar文件。我已经阅读了很多类似的线程,但仍然无法使其工作。如果我从包含该文件的文件夹运行“java -jar jar_name.jar”,我可以从cmd行运行它。我运行了应该修复文件关联的Jarfix程序,但它仍然不起作用。当我从Eclipse导出时,它告诉我

  

JAR导出已完成警告。有关其他信息,请参阅详细信息....使用编译警告导出:Shutdown / src / Shutdown.java

从我所看到的,这不是一个真正的问题,它只是意味着你的程序中有警告。我不认为代码有任何问题,但它是一个小程序,所以我还是把它包括在内。任何帮助表示赞赏。

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.");
        }

    }
}

2 个答案:

答案 0 :(得分:1)

 Select your File/Project -- Export -- Runnable JAR -- Select class with main() in 
Launch Configuration -- destination
  • 如果您想在生成的JAR中为您的类添加依赖项,请选择 第一个选项(将所需的库提取到生成的JAR中)
  • 如果您不需要它们而只需要您的代码,请选择第3个选项(复制 将所需的库放入生成的JAR旁边的子文件夹中

希望这会有所帮助:)

答案 1 :(得分:0)

在Windows上,标准Java安装会将.jar文件与javaw.exe程序双击,而不是java.exe。区别在于javaw.exe没有控制台(命令窗口)。

由于您没有控制台,因此您没有System.in。我没有机会尝试它,但我的猜测是尝试从System.in读取将导致立即到达流的末尾,这将导致Scanner的下一个*方法抛出NoSuchElementException。

我总结一下,你不能使用System.in。如果要提示用户输入,请使用Swing或JavaFX。

附加说明:您不能在Java中使用==!=来比较字符串,因为这些运算符会比较对象引用而不是比较字符串值。请改用equals方法。例如:

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