简体   繁体   English

如何将Java文件编译到与Java文件相同级别的目录中?

[英]How to compile java files into directory at the same level as java files?

I have very simple case. 我有一个非常简单的案例。 In folder D://redbacktree I have two files .java . 在文件夹D://redbacktree我有两个文件.java

I want to compile them into folder D://redbacktree/bin . 我想将它们编译到文件夹D://redbacktree/bin

What I tried is : 我试过的是:

javac -d bin *.java

This created folder redblacktree inside bin folder and put .class files into D://redbacktree/bin/redblacktree/ 这将在bin文件夹中创建文件夹redblacktree并将.class文件放入D://redbacktree/bin/redblacktree/

But I don't want javac to create redblacktree folder inside bin folder. 但是我不希望javacbin文件夹内创建redblacktree文件夹。

How to compile and run this correctly? 如何正确编译并运行此程序?

Maybe your java files have a package declaration? 也许您的Java文件具有包声明?

package redbacktree;

The compiler creates a separate folder for each package. 编译器为每个软件包创建一个单独的文件夹。 You could try to use the default package (ie omit the package declration completely), although it is not a good practice to use the default package. 您可以尝试使用默认程序包(即完全忽略程序包的斜率),尽管使用默认程序包不是一个好习惯。

I'll create an answer from my comment instead. 我将根据评论创建答案。 So first off; 所以首先 if you have package declarations (which is good practice to organize your code and modules) your files will end up in a folder structure matching your package structure. 如果您有程序包声明(这是组织代码和模块的良好做法),则文件将最终位于与程序包结构匹配的文件夹结构中。

I'd suggest reading the man pages for javac and java to see all the flags and options. 我建议阅读javacjava的手册页,以查看所有标志和选项。 I'd also suggest using some kind of build tool and follow the java "convention" of how to organize your source and class files. 我还建议您使用某种构建工具,并按照Java的“惯例”来组织源文件和类文件。 Read more here . 在这里阅读更多。

Maven , Gradle or Buildr are three examples of build tools for Java. MavenGradleBuildr是用于Java的构建工具的三个示例。

Too "fix" your current problem you can run your compiled java classes using: 太“解决”您当前的问题,您可以使用以下命令运行已编译的Java类:

java -cp bin redblacktree.<name of your class>

ie you need to use the fully qualified name (package path + class name) when telling java which class to run. 也就是说,当告诉java要运行哪个类时,您需要使用标准名称(包路径+类名称)。 Also -cp specifies the (root-)classpath, where your compiled classes can be found. -cp还指定(root-)classpath,可在其中找到已编译的类。

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

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