如何在命令提示符下运行由 intellij 创建的 java 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27108911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:50:06  来源:igfitidea点击:

How to run java program in command prompt,created by intellij

javaintellij-idea

提问by ashTon

How do I run my Java program in command prompt, my project was created in Intellij, and I am having difficulties running it in the command prompt...without using the Intellij in creating project,I can run the java program in command prompt.

我如何在命令提示符下运行我的 Java 程序,我的项目是在 Intellij 中创建的,但我在命令提示符下运行它时遇到了困难......在创建项目时不使用 Intellij,我可以在命令提示符下运行 Java 程序。

I do this like this.

我这样做。

java myjava ->this would work.

but the project created by Intellij,this is the path.

但是Intellij创建的项目,这是路径。

C:\myjava\sampl1\src\com\myexample\test>

when I issue this command

当我发出这个命令时

java myjava -> Error: Could not find or load main class myjava

but I am inside in that directory.

但我在那个目录里面。

Thank you in advance.

先感谢您。

采纳答案by Boann

Three issues:

三个问题:

  1. You have to specify the fully qualified class name (that means including the package name) to the javacommand. It looks like your myjavaclass is in a package com.myexample.test. So its fully qualified name is com.myexample.test.myjava.

  2. When you run the javacommand you have to be in the directory that is at the base of the package hierarchy (or put that directory on the classpath).

  3. You're using the srcdirectory, which contains .javasource files, but the javacommand expects compiled .classfiles, so you need to use the project's output directory. Its location in your project will depend on your IDE and configuration but it will contain same-named structure as inside src, except with .classfiles instead of .javafiles.

  1. 您必须为命令指定完全限定的类名(这意味着包括包名)java。看起来您的myjava课程在一个包中com.myexample.test。所以它的完全限定名称是com.myexample.test.myjava.

  2. 当您运行该java命令时,您必须位于位于包层次结构基础的目录中(或将该目录放在类路径上)。

  3. 您正在使用src包含.java源文件的目录,但该java命令需要编译.class文件,因此您需要使用项目的输出目录。它在您的项目中的位置将取决于您的 IDE 和配置,但它将包含与 inside 同名的结构src,除了.class文件而不是.java文件。

In your case, navigate to:

在您的情况下,导航到:

C:\myjava\sampl1\out\production\

Then run:

然后运行:

java com.myexample.test.myjava

回答by Reimeus

It looks like the class is in a package com.myexample.test. Try running

看起来这个类在一个包中com.myexample.test。尝试跑步

java com.myexample.test.myjava

from the project's bindirectory

从项目bin目录

回答by Oscar Albert

I hope this can help somebody, a little late but, just I had this problem, ok my solution it next: 1. Run your code normally and copy the command line that the IntellijIDEA made, see the screenshot.

我希望这可以帮助某人,有点晚了但是,只是我遇到了这个问题,接下来我的解决方案是: 1. 正常运行您的代码并复制 IntellijIDEA 制作的命令行,请参阅屏幕截图。

IntellijIDEA program running to copy the command line

IntellijIDEA程序运行复制命令行

Copying the command line

复制命令行

2.Copy and paste the command line that it uses to use with your params.

2.复制并粘贴它用于与您的参数一起使用的命令行。

Adding my own param and that's all.

Adding my own param and that's all.

回答by Kensam

From intellij, open your project structure, and click on Modules. You'll be able to see the output path. Usually, it's something like (if on mac):

在 intellij 中,打开您的项目结构,然后单击 Modules。您将能够看到输出路径。通常,它类似于(如果在 mac 上):

~/../out/production/{context}.

Back to your example in windows. The path you gave is, C:\myjava\sampl1\src\com\myexample\test>, so you have your project in : C:\myjavadirectory where your context (or project name) is sampl1. That means that your output (*.class files) will be in

回到你在 Windows 中的例子。您提供的路径是 C:\myjava\sampl1\src\com\myexample\test>,因此您的项目位于 : C:\myjava上下文(或项目名称)所在的目录中sampl1。这意味着您的输出(*.class 文件)将在

C:\myjava\sampl1\out\production\sampl1

Navigate into the folder and then run the command: java com.myexample.test.myjava

导航到文件夹,然后运行命令: java com.myexample.test.myjava