简体   繁体   中英

How to build a java project using the command line?

I am a student and I hate not knowing how things are organized. I'd like to be able to create a full on java project from scratch on the command line. I'd like to be able to import jars and set the classpath, make packages and import them. ALso learn about environment variables. I currently do not know much about organizing code. I just know how to code in Java.

Is there a textbook, online article or the like that allows one to learn how to organize a java project?

I do not want any involvement with eclipse or any IDE. I am willing to learn Maven, XML, or the likes to accomplish my goal.

If you are a student willing to have a Java programming career, it might help to learn how to do things from command line, eg edit the files, compiling the classes, testing and building the project. Oracle tutorials provide example on this matter: https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html#win32-2

However, I strongly advise you to embrace an IDE as your Java career will mostly reside in an IDE as real life projects are BIG! There are tons of helpful things the IDE does to you out of the box or to simplify things. Since you are a student, I will give you one basic example besides compiling: a class with 10 fields requires you some typing for getters, setters, hashCode, equals. Alternative? Few keystrokes to instruct the IDE to generate them for you. And that's one basic example.

Regarding the project structure, embracing the (since you mentioned it) Maven project structure of src/main/{java,resources}, src/test/{java,resources} even if you do NOT use Maven. This will let you forget about organizing the files around.

If you were asking about structuring the classes in the right packages, you will figure out yourself as you gain experience. Rule of thumb is to group classes together by functionality they provide. Additionally, if the packages are organized right, if you change something and touching a few classes, ideally you'd want the changed classes to be located in a single package if possible.

Learning Maven is a good choice as it is a powerful tool for building a project and keeping things organized (project structure, project dependencies, etc.).

A simple Java program can be compiled trivially by javac MyMainClass.java , provided that your CLASSPATH list directories and jars with its dependencies.

Compiling a large Java project is not trivial. There are several tools intended to make it simpler.

  • Gradle : very widely used, uses its own language, very powerful and complex.
  • Maven : Still widely used. Uses XML to describe everything.
  • Apache Ant is lower level and lower abstraction power.

The power of these tools lies exactly in hiding the boilerplate of the Java project building process. They generate a skeleton of a build for you, and provide higher-level operations.

Of course you can start with simplest and watch the steps these tools make.

Reading the docs for javac and jar thoroughly does help, too.

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