简体   繁体   中英

Java and compiling when not using a IDE

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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