简体   繁体   English

Java和不使用IDE时进行编译

[英]Java and compiling when not using a IDE

I understand in java that you are forced into a single file per class. 我在Java中了解到,每个类都被强制放入一个文件中。

So if I have classes like: 因此,如果我有类似的课程:

/my_project/main.java
/my_project/classes/user.java
/my_project/classes/other.java

And my main.java references the user and other files, how would I compile this via the command line? 我的main.java引用了用户和其他文件,我将如何通过命令行对其进行编译?

If I was to have external .jar's that I was referencing, and I placed them in a particular folder, how could I also include this in my compiling? 如果要使用我正在引用的外部.jar,并将它们放在特定的文件夹中,我该如何在编译时也将其包括在内? (or is there a general place I can put them where they will be picked up automatically like how python does this) (或者是否有一个一般的地方,我可以将它们放置在它们将被自动拾取的地方,例如python这样做的方式)

to compile, you will need to specify each source file, from the my_project folder: 要进行编译,您需要从my_project文件夹中指定每个源文件:

javac classes/user.java classes/other.java main.java

You can also specify jar files for your classpath with the -cp option: 您还可以使用-cp选项为类路径指定jar文件:

javac -cp myjarfile.jar main.java

You may also need to fiddle with the -cp flag to make sure your classes folder is in the classpath. 您可能还需要弄弄-cp标志,以确保您的classes文件夹在classpath中。

First of all it's poor style to make Java classes starting with lowercase. 首先,使Java类以小写字母开头是不好的样式。

Only public classes need to be in their own file, but you can add as many package-private classes as you like to the same file (although this is seen as poor style). 只有公共类需要在其自己的文件中,但是您可以根据需要向同一文件中添加任意多个包私有类(尽管这被认为是较差的样式)。

That said, the easiest way would to compile your code would be: 也就是说,编译代码的最简单方法是:

javac /my_project/main.java /my_project/classes/user.java /my_project/classes/other.java

In any case, proper code layout should be that classes are in a directory structure matching their package. 无论如何,正确的代码布局应是使类位于与它们的包相匹配的目录结构中。

EDIT : There is a fairly good explanation of conventions here http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter05/packagesImport.html 编辑 :这里有一个相当不错的约定解释, http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter05/packagesImport.html

除了上述答案之外,您还可以使用Apache Ant之类的工具 ,以简化构建的配置(如果复杂)。

Look at the documentation for javac . 查看javac文档 You can pass multiple source files, or specify the source directory. 您可以传递多个源文件,或指定源目录。

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

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