简体   繁体   English

当它包含一个外部Java程序包时,如何在命令行上编译我的主Java程序?

[英]How do I compile my main Java program on the command line when it includes an external java package?

I'm a relative Java newbie so apologies if the question appears somewhat basic. 我是一个相对的Java新手,所以如果问题看起来有些基本,我深表歉意。 I've googled high and low for an answer here and I'm not finding anything that's helping. 我在这里搜寻了一个高低的答案,但找不到任何有帮助的东西。

Problem: 问题:

Whilst I'm able to integrate external packages into my Java programs from an IDE environment, I am trying to do run a very basic program from the command line that calls on a separate, basic package file that I have written - and am simply doing all this as I want to have a bottom-up understanding of how package files are related to a main program by Java. 虽然我可以从IDE环境将外部程序包集成到Java程序中,但是我试图通过命令行运行一个非常基本的程序,该程序调用我编写的单独的基本程序包文件,并且只是在做所有这些都是因为我想对Java中的包文件与主程序之间的关系有一个自下而上的了解。

I have a program that sits on my desktop named MyProgram.java: 我的桌面上有一个名为MyProgram.java的程序:

import org.somepackage;

public class MyProgram {

    public static void main(String arguments[]) {

    System.out.println("Programme up and running...");
    Human myHuman = new Human(); 
    myHuman.scream();

  }

Still on the Desktop, I then have another folder which I've named src , inside of which I have created the necessary subfolders corresponding to the package name, ie ./src/org/somepackage - and in this location, I have the Human.java file which defines the Human class with the following contents: 仍然在桌面上,我还有一个名为src的文件夹,在其中创建了与包名称相对应的必要子文件夹,即./src/org/somepackage-在此位置,我拥有了Human .java文件,其中定义了Human类,其内容如下:

package org.somepackage;

public class Human {

    public void scream() {

    System.out.println("I exist!!");

  }

}

I then created a classes folder, again on the Desktop, and ran the following compile command on the command line: 然后,我再次在桌面上创建了一个classes文件夹,并在命令行上运行了以下编译命令:

javac -d ./classes/ ./src/org/packagename/Human.java

This ran fine and created - as expected - the Human.class file within the ./classes/org/packagename/ location. 运行正常,并按预期方式在./classes/org/packagename/位置中创建了Human.class文件。

However, where I fall down is when I then try to compile MyProgram.java on the command line, ie 但是,我失败的地方是我随后尝试在命令行上编译MyProgram.java的时候,即

javac -cp ".:./classes/" MyProgram.java

As you'll see, my class path contains a reference to the current location ( "." ) for the MyProgram.java file, and it contains a reference to the classes folder ( "./classes/" ) which is the base location for the org.somepackage package inside whose subfolders ( ./classes/org/somepackage/ ) on can find the Human.class file. 如您所见,我的类路径包含对MyProgram.java文件当前位置( “。” )的引用,并且包含对类文件夹( “ ./classes/” )的引用,这是基本位置对于其中子文件夹( ./classes/org/somepackage/ )所在的org.somepackage软件包,可以找到Human.class文件。

At this stage, I was simply expecting the java engine to compile MyProgram.java into the program MyProgram.class - but, instead, I get an error: 在此阶段,我只是希望Java引擎将MyProgram.java编译成程序MyProgram.class-但是,出现了一个错误:

MyProgram.java:1: error: package org does not exist MyProgram.java:1:错误:软件包org不存在

I've been following the instructions listed here: 我一直按照这里列出的说明进行操作:

https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html

and I don't appear to be deviating from the instructions - yet I'm unable to locate an explanation on Stackoverflow or anywhere else as to a possible reason for this compile failure. 而且我似乎并没有偏离指令-但我无法在Stackoverflow或其他任何地方找到有关此编译失败的可能原因的解释。 If anyone has an idea, your help would be very much appreciated. 如果有人有想法,将非常感谢您的帮助。

Thanks, 谢谢,

Your mistake is here 你的错误在这里

import org.somepackage; <--

public class MyProgram {

    public static void main(String arguments[]) {

    System.out.println("Programme up and running...");
    Human myHuman = new Human(); 
    myHuman.scream();

  }

you forgot to import class actually, you need to write this name import org.somepackage.Human; 您实际上忘记了导入类,您需要写这个名字import org.somepackage.Human; import all package content import org.somepackage.*; 导入所有包内容import org.somepackage.*; or write full qualified name of class in your code 或在代码中写出类的完整合格名称

org.somepackage.Human myHuman = new org.somepackage.Human(); 
myHuman.scream();

correct mistake: 正确的错误:

import org.somepackage.Human;

public class MyProgram {

    public static void main(String arguments[]) {

    System.out.println("Programme up and running...");
    Human myHuman = new Human(); 
    myHuman.scream();

  } 

after that compile your Human.java by this command: 之后,通过以下命令编译Human.java:

javac -d classes Human.java

and MyProgram.java 和MyProgram.java

javac -d classes -cp "classes" MyProgram.java

and run MyProgram by 并通过运行MyProgram

java -cp "classes" MyProgram

暂无
暂无

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

相关问题 在命令行中用 external.jar 编译 java 程序 - Compile java program in command line with external .jar 在Windows命令行上,如何将cat的输出输入到java main方法中? - On the Windows command line, how do I feed output from cat into my java main method? 如何在Mac上编译和运行Java程序? - How do I compile and run a program in Java on my Mac? 如何将JavaDB数据库集成到我的主java包中 - How do I integrate a JavaDB database into my main java package 编译程序时出现错误(线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException) - I get the Error when i compile my program (Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException) 如何将我的java程序转换为linux上的命令行实用程序? - How do I turn my java program into a command line utility on linux? 如何使我的Java程序接受命令行中提供的excel文件? - How do I make my java program to accept an excel file given in the command line? 如何在Windows 7命令行上执行.jar java程序? - How do I execute .jar java program on Windows 7 command line? "如何在 Windows 上从命令行运行 Java 程序?" - How do I run a Java program from the command line on Windows? 如何在 VScode 中制作脚本,以便当我按下某个键时,它会在外部终端中运行并编译 Java 程序? - How do I make a script in VScode so that when I press a key it will run and compile a Java program in an external terminal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM