简体   繁体   English

Java程序无法从命令提示符执行

[英]Java program unable to execute from command prompt

I recently installed a java software in my PC and started writing some simple programs. 我最近在我的电脑上安装了一个java软件,开始编写一些简单的程序。 I didn't face any problem while compiling the programs, but while executing it, it shows this error message - 编译程序时我没有遇到任何问题,但在执行时,它显示了此错误消息 -

"Windows can't open this file. “Windows无法打开此文件。

File: HelloWorld.java 文件:HelloWorld.java

To open this file, windows need to know what program you want to open it. 要打开此文件,Windows需要知道要打开它的程序。 Windows can go online to lookup it automatically, or you can manually select from a list of programs that are installed on your computer. Windows可以联机自动查找,也可以从计算机上安装的程序列表中手动选择。

I know all the path settings are correct. 我知道所有的路径设置都是正确的。 In fact, there was no problem at all while compiling the program. 事实上,在编译程序时完全没有问题。 What can be the problem of this? 这可能是什么问题? I even reinstalled JRE and that didn't help. 我甚至重新安装了JRE,但没有帮助。 Can someone help me? 有人能帮我吗?

Note: I'm using Windows 7 64 bit architecture OS and I'm using command prompt for compilation and execution of the file. 注意:我正在使用Windows 7 64位架构操作系统,我正在使用命令提示符来编译和执行该文件。

I'll assume that you've double-clicked the .java file, eg in the file explorer. 我假设您双击了.java文件,例如在文件资源管理器中。 A java source file isn't a (click-launchable) executable, and - without some acrobatics - neither is a compiled .class file: you shouldn't expect to double-click either and start your program. java源文件不是(可点击启动)可执行文件,并且 - 没有一些杂技 - 也不是编译的.class文件:您不应期望双击任何一个并启动您的程序。

In order to get this sort of behavior, you'll need to build a launchable program, and there are a few ways to do this. 为了获得这种行为,您需要构建一个可启动的程序,并且有几种方法可以做到这一点。 One is by making a batch file that runs the java VM with your code, another is creating an executable jar file. 一种方法是制作一个用你的代码运行java VM的批处理文件,另一种是创建一个可执行的jar文件。

To just run your code outside your IDE, you can invoke the java VM on the command line: 要在IDE外部运行代码,可以在命令行上调用java VM:

c:\> java HelloWorld

As to your specific error message, you haven't associated any program with .java files. 至于您的特定错误消息,您尚未将任何程序与.java文件相关联。 Typically, as programmers, we want this Windows file association to be our editor of choice or our IDE. 通常,作为程序员,我们希望这个Windows文件关联成为我们的首选编辑器或IDE。 You can create this association by Right-clicking on the file, choosing Properties from the menu and then clicking the Change button beside Opens with: to pick an application. 您可以通过右键单击文件,从菜单中选择“ 属性 ”,然后单击“ 打开方式”旁边的“ 更改”按钮来选择应用程序来创建此关联。

But this is a side-issue: you still won't use this to make .java file executable. 但这是一个副问题:您仍然不会使用它来使.java文件可执行。 Search around this site for questions and answers about building executable jar files. 搜索此站点以获取有关构建可执行jar文件的问题和解答。 If you're using a specific IDE like Eclipse or NetBeans, use that to refine your search. 如果您使用的是Eclipse或NetBeans等特定IDE,请使用它来优化搜索。

When you want to execute the program, you specifiy the class name rather than the file name: 如果要执行程序,请指定名而不是文件名:

java HelloWorld

Don't use java HelloWorld.java , for instance. 例如,不要使用java HelloWorld.java

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

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