简体   繁体   English

从命令行编译并运行具有复杂文件结构的Java程序

[英]Compiling and running a java program with complex file structure from the commandline

Trying to compile and run my java program from the commandline that is set up a bit weird. 尝试从设置有点奇怪的命令行编译和运行我的Java程序。 The file structure is as follows: 文件结构如下:

[ROOT]/
|
|____libs/
|    |____myExtraJar.jar
|     
|____src/
     |____main/
          |____com/
               |____example/
                    |____myClass.java

The package is defined at the top of the java file as 该软件包在java文件的顶部定义为

package com.example;

I am able to compile the program fine (I think) while in the root folder, using 我可以在根文件夹中使用以下命令很好地编译程序(我认为)

javac -classpath "/libs/myExtraJar.jar" src/main/com/example/*.java

I don't get any compilation errors (such the ones that occur if I leave off the classpath) and I can see that .class files are being created in the com/example/ folder. 我没有收到任何编译错误(例如,如果我离开类路径会发生这些错误),并且可以看到在com / example /文件夹中正在创建.class文件。 However, I can't find any way to run the compiled program. 但是,我找不到任何运行已编译程序的方法。 Running 运行

java src/main/com/example/myClass

results in the message 结果消息

Error: Could not find or load main class src.main.com.example.myClass

Any help would be appreciated. 任何帮助,将不胜感激。

You need to specify the classpath when you run it, and you also need to use the fully-qualified classname. 您需要在运行时指定类路径,并且还需要使用完全限定的类名。 Like, 喜欢,

java -cp "libs/myExtraJar.jar:src/main" com.example.myClass

Elliot is right. 艾略特是对的。 More precisely, you need to add the build directory to your classpath. 更准确地说,您需要将构建目录添加到类路径中。 It is the directory containing your *.class files, and is sometimes named target/ . 它是包含*.class文件的目录,有时称为target/

$ java -cp "target:lib/myExtraJar.jar" com.example.myClass

Moreover, src/main/com/example/myClass should be com.example.myClass , which is the fully-qualified class name . 此外, src/main/com/example/myClass应该是com.example.myClass ,这是完全限定的类名 See http://www.manpagez.com/man/1/java/ for details of the java command. 有关java命令的详细信息,请参见http://www.manpagez.com/man/1/java/

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

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