简体   繁体   English

无法运行jar文件或Cmd

[英]Can't run the jar file nor in Cmd

I am totally new to programming.I have just started Java and I currently work on BlueJ.My OS is windows.I don't know much about coding.So basically I just made a program.Here's it 我是编程的新手,我刚开始Java的工作,现在在BlueJ上工作,我的操作系统是Windows,我对编码不太了解,所以基本上我只是写了一个程序。

class  Vedant {
    public static void main(String[] args) {
        int a;
        a = 1;
        if (20>a);
        a = 5;

        {
            System.out.println("Number ="+a);
        }
    }
}

I saved this as a Jar file.When I double click on the jar file it doesn't open.I have installed the latest version of Java.If I open with cmd I get an error:Unable to access jar file. 我将其保存为Jar文件。当我双击jar文件时,它没有打开。我已经安装了最新版本的Java。如果我使用cmd打开,则会收到错误消息:无法访问jar文件。 I tried converting the jar file to exe using Launch4J but the exe file doesn't open.Any Help 我尝试使用Launch4J将jar文件转换为exe,但是exe文件无法打开。

And yea I am a Extreme Beginner in Java so I don't understand much of it. 是的,我是Java的极端入门者,所以我不太了解它。

You will need to open the file using a terminal. 您将需要使用终端打开文件。

As you are a windows user, you can use command prompt . 如果您是Windows用户,则可以使用命令提示符

  1. Open Command Prompt by pressing Win+R , type cmd and press Enter . 通过按Win+R打开命令提示符,键入cmd并按Enter
  2. Use this command for running your file in command prompt. 使用此命令在命令提示符下运行文件。

    java -jar (Full path to jar file) java -jar(jar文件的完整路径)

If you get an error showing that java cannot be found, then make sure that you have java installed in your PC. 如果收到显示无法找到Java的错误消息,请确保PC中已安装Java。

If installed, set your Java Path in your System Environment Variables. 如果已安装,请在系统环境变量中设置Java路径。

Step by step process on how to set your Java path is given here . 这里给出有关如何设置Java路径的分步过程。

After setting your java path, you will be able to run your jar file using the command. 设置Java路径后,您将能够使用以下命令运行jar文件。

Points to keep in mind: 注意事项:

  • While creating Jar file from BlueJ select main class as "Vedant" from the drop down list. 从BlueJ创建Jar文件时,从下拉列表中选择主类作为“ Vedant”。 By default, none(cannot be executed) is selected. 默认情况下,没有选择(无法执行)。

看图片

After that it can successfully be executed: (Replace C:\\Users\\Midhun\\Desktop\\vedant.jar with the link to your jar file) 之后,它可以成功执行:(将C:\\Users\\Midhun\\Desktop\\vedant.jar替换为jar文件的链接)

C:\Users\Midhun>java -jar C:\Users\Midhun\Desktop\vedant.jar
Number =5

If you don't select the main class while creating the jar file, you will get the following error. 如果在创建jar文件时未选择主类,则会出现以下错误。

C:\Users\Midhun>java -jar C:\Users\Midhun\Desktop\vedant.jar
Error: Could not find or load main class 

You can overcome this error by either recreating jar with main class or executing the following command 您可以通过使用主类重新创建jar或执行以下命令来克服此错误

C:\Users\Midhun>java -cp C:\Users\Midhun\Desktop\vedant.jar Vedant
Number =5

C:\\Users\\Midhun\\Desktop\\vedant.jar is the full path to jar file and Vedant is the name of main class that you want to run from the jar file. C:\\Users\\Midhun\\Desktop\\vedant.jar是jar文件的完整路径,而Vedant是要从jar文件运行的主类的名称。

If you give the wrong link to the jar file you will get the following error as mentioned in your question. 如果您给jar文件提供了错误的链接,您将得到问题中提到的以下错误。

C:\Users\Midhun\Desktop>java -jar C:\Users\Midhun\Desktop\vedan.jar
Error: Unable to access jarfile vedan.jar

You can create a .cmd file that includes the following text: java -jar path_for_your_jar_file 您可以创建一个包含以下文本的.cmd文件:java -jar path_for_your_jar_file

save it and just click on that file. 保存它,然后单击该文件。 It will launch your program (this is the procedure instead of entering each time to the command prompt). 它将启动您的程序(这是步骤,而不是每次都输入命令提示符)。

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

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