繁体   English   中英

Jar文件无法双击运行

[英]Jar file won't run with double click

我无法运行.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.");
        }

    }
}
 Select your File/Project -- Export -- Runnable JAR -- Select class with main() in 
Launch Configuration -- destination
  • 如果要在生成的JAR中获得类的依赖关系,请选择1st选项(将所需的库提取到生成的JAR中)
  • 如果只需要代码,就不需要它们,请选择第3个选项(将所需的库复制到生成的JAR旁边的子文件夹中)

希望这可以帮助 :)

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

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

摘要,您不能使用System.in。 如果要提示用户输入,请使用Swing或JavaFX。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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