开发者

Java and compiling when not using a IDE

开发者 https://www.devze.com 2023-03-13 17:25 出处:网络
I understand in java that you are forced into a single file per class. So if I have classes like: /my_project/main.java

I understand in java that you are forced into a single file per class.

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?

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? (or is there a general place I can put them where they will be picked up automatically like how python does this)


to compile, you will need to specify each source file, from the my_project folder:

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

You can also specify jar files for your classpath with the -cp option:

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.


First of all it's poor style to make Java classes starting with lowercase.

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


In addition to the above answer, you can use something like Apache Ant, for easier configuration of your build (if it gets complicated).


Look at the documentation for javac. You can pass multiple source files, or specify the source directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号