简体   繁体   English

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

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

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.先感谢您。

Three issues:三个问题:

  1. You have to specify the fully qualified class name (that means including the package name) to the java command.您必须为java命令指定完全限定的类名(这意味着包括包名)。 It looks like your myjava class is in a package com.myexample.test .看起来您的myjava类位于com.myexample.test包中。 So its fully qualified name is com.myexample.test.myjava .所以它的完全限定名称是com.myexample.test.myjava

  2. When you run the java command you have to be in the directory that is at the base of the package hierarchy (or put that directory on the classpath).当您运行java命令时,您必须位于位于包层次结构基础的目录中(或将该目录放在类路径上)。

  3. You're using the src directory, which contains .java source files, but the java command expects compiled .class files, so you need to use the project's output directory.您正在使用src目录,其中包含.java源文件,但java命令需要编译的.class文件,因此您需要使用项目的输出目录。 Its location in your project will depend on your IDE and configuration but it will contain same-named structure as inside src , except with .class files instead of .java files.它在您的项目中的位置将取决于您的 IDE 和配置,但它将包含与src内部同名的结构,除了.class文件而不是.java文件。

In your case, navigate to:在您的情况下,导航到:

C:\myjava\sampl1\out\production\

Then run:然后运行:

java com.myexample.test.myjava

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程序运行复制命令行

复制命令行

2.Copy and paste the command line that it uses to use with your params. 2.复制并粘贴它用于与您的参数一起使用的命令行。

添加我自己的参数,仅此而已。

It looks like the class is in a package com.myexample.test .看起来这个类在一个包com.myexample.test Try running尝试跑步

java com.myexample.test.myjava

from the project's bin directory从项目的bin目录

From intellij, open your project structure, and click on Modules.在 intellij 中,打开您的项目结构,然后单击 Modules。 You'll be able to see the output path.您将能够看到输出路径。 Usually, it's something like (if on mac):通常,它类似于(如果在 mac 上):

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

Back to your example in windows.回到你在 Windows 中的例子。 The path you gave is, C:\\myjava\\sampl1\\src\\com\\myexample\\test>, so you have your project in : C:\\myjava directory where your context (or project name) is sampl1 .您提供的路径是 C:\\myjava\\sampl1\\src\\com\\myexample\\test>,因此您的项目位于: C:\\myjava目录中,您的上下文(或项目名称)是sampl1 That means that your output (*.class files) will be in这意味着您的输出(*.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

In my terminal the current directory is the root project directory named Practice.在我的终端中,当前目录是名为 Practice 的根项目目录。
When you issue the java command make sure to specify the classpath using the -cp flag.当您发出 java 命令时,请确保使用 -cp 标志指定类路径。
The classpath will extend from your project root directory through out/production/<project name> .类路径将从您的项目根目录扩展到out/production/<project name>

For example, my classpath was:例如,我的类路径是:
-cp /Users/ashton/java/IdeaProjects/Practice/out/production/Practice

Then you add a space, then the package name including class name you want to run, then any parameters.然后添加一个空格,然后是包名,包括要运行的类名,然后是任何参数。 So the entire command including parameters was this:所以包括参数的整个命令是这样的:
java -cp /Users/ashton/java/IdeaProjects/Practice/out/production/Practice us.debie.ian.ljbe.FileSearchApp one two three

You should be able to piece it all together by examining the screenshot below to see what current directory is in, what classpath was specified, etc.您应该能够通过检查下面的屏幕截图来查看当前目录所在的位置、指定的类路径等,将它们拼凑在一起。

智能截图

If you were using a Maven project for example, Just locate the relevant .class file under /target folder.例如,如果您使用的是 Maven 项目,只需在/target文件夹下找到相关的.class文件。 Then execute it using 'java' command like you would typically run any application.然后像通常运行任何应用程序一样使用“java”命令执行它。

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

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