-5

The build tools I usually use are Make, Ninja or NPM scripts. But... what can I use with Java, which I can just slap on my project to have a reliable "just build and run this" method, that'll work on Windows and macOS? I could use shell scripts - but those don't work on Windows, so I have to do batch files just for Windows.

Also, I know I could use Make here, but unfortunately Java does not do "object files" like C++ - and defining where outputs go is also not as easy either (to me).

So having a build tool that natively knows how javac and java behave would be really useful, especially since I don't know their behaviour at all.

To give a little example, consider this tree:

.
├── [ 425]  oop11.iml
└── [  96]  src
    └── [  96]  de
        └── [  96]  uni_marburg
            └── [  96]  wise1819
                └── [ 160]  ue11
                    ├── [3.7K]  Card.java
                    ├── [1023]  Lambdas.java
                    └── [ 138]  Main.java

Lambdas.java and Main.java both contain a public static void main method - so I would need two targets - one that does Lambdas.java and one that does Main.java and Card.java.

How can I achieve this? Is there a build tool that can work with this?

grooveplex
  • 2,492
  • 4
  • 28
  • 30
Ingwie Phoenix
  • 2,703
  • 2
  • 24
  • 33
  • 2
    Normally I would suggest [Ant](https://ant.apache.org), but since you seem to detest Java, you might be better off simply creating bash/bat files containing `javac` and `java` commands. – VGR Jan 30 '19 at 00:55
  • 1
    _Java does not do "object files" like C++_ - Java has `.class` files which are approximately the same thing. – dave Jan 30 '19 at 00:57
  • @dave So doing `javac Foo.java && javac Bar.java && javac Foo.class Bar.class` would work? I know the command isn't exactly right, but i guess you get the gist of it... – Ingwie Phoenix Jan 30 '19 at 00:59
  • @VGR It's not that I detest Java - I just have no idea about it at all and I know that I suck at learning a new language - and with Java's specific features in regards to modularism and such, it very well is quite different to C++. I will give Ant a shot and see how it plays out. :) – Ingwie Phoenix Jan 30 '19 at 01:01
  • 1
    Not quite. You use `javac` to produce `.class` files from `.java` files. The next step would typically be to package the `.class` files in a `.jar` file using the `jar` command. – dave Jan 30 '19 at 01:01
  • @dave So essentially the `jar` program is what a linker like `ld` is for C/C++? – Ingwie Phoenix Jan 30 '19 at 01:02
  • I think the simplest for you would be building an artifact with Intellij itself, which does not require knowledge of build systems like Ant, Maven, Gradle, etc - check "Packaging the application in a JAR" section at https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html#package – yegodm Jan 30 '19 at 01:39
  • @VGR Ant is obsolete. Maven and Gradle are the current standard build tools. – chrylis -cautiouslyoptimistic- Jan 30 '19 at 01:43
  • @chrylis That is not even remotely true. I could list six counterpoints off the top of my head. But a religious war here is not productive. – VGR Jan 30 '19 at 02:37

2 Answers2

3

Maven or Gradle are the most common Java build tools. You just need to add a file defining your project (pom.xml for Maven) and follow a standard directory layout for your sources. E.g:

.
├── pom.xml
└── src
   └── main
     └── java
        └── de
          └──  uni_marburg
            └──  wise1819
                └──  ue11
                    ├──  Card.java
                    ├──  Lambdas.java
                    └──  Main.java

You can use maven's exec plugin to execute in a single command. For instance:

mvn exec:java -DmainClass=de.your-package.Lamdba

Finally, to make the set-up simpler for your students, you could also use maven wrapper, which provides a series of scripts to install the required Maven version.

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
  • 1
    I suggest adding a mention of the Maven archetype for small simple projects, also useful to a beginner: [`maven-archetype-quickstart`](https://maven.apache.org/archetypes/maven-archetype-quickstart/). Found also [here](https://mvnrepository.com/artifact/org.apache.maven.archetypes/maven-archetype-quickstart). – Basil Bourque Feb 06 '19 at 06:43
3

Gradle, Ant and Maven all have a steep learning curve, especially if you are not familiar with any of them and not used to working in the Java ecosystem. I mean, it's easy to find 'Hello World' tutorials for those, and they are - of course - fully documented in manuals. But if you want to do something more than 'Hello World', something which "should" be simple, that's when they become hard (until you are expert, which takes time).

Most Java experts able to help you by answering this question have years of experience in Gradle, Ant, Maven and one of the IDEs - so expert that they have forgotten the early days pain.

The real purpose and power of Gradle or Ant is to carry out all build steps of a complex project, automatically. They are batch or script processors tailored to all the things you need to do when building large Java projects - Gradle is more specific to build steps, Ant is more general, but either one could be used. For example, I use Gradle for an open source project where I need to fetch the latest source code from two different Github projects, check the libraries are up-to-date, update the build number, and build two different binaries being a "production" version and a "development" version, and pack them into .jar files - and I create builds like this over and over, every time the code changes. It can take many hours of coding time just to get the gradle set up exactly right to automate this type of thing.

It's probably not worth using Gradle or Ant for building a simple project. As others have said, the command line compiler javac is enough for a simple project.

The best advice I can give is "bite the bullet". Take the trouble to learn the basics properly. Install and use one of the IDEs (Eclipse or IntelliJ).

Then you will find creating new Java projects is quick and easy. The IDEs are suited for both small and large projects.

Golden tip: Install Maven and use it from day 1, make all your projects Maven projects. If copying other people's code from tutorials or open source projects, pay attention to the dependencies and their versions, and copy the dependencies into the Maven pom.xml for your own project. Dependencies = libraries.


Since part of your question appears to be "is it worth me learning these tools for this language which I don't yet know" I will set out some general advantages and disadvantages of Java.

Advantages of Java:

  • exact same code runs on Windows, Mac, or Linux (ok you might need to add native libraries for a few things like direct access to sound or graphics if you are coding games, but these libraries are readily available)
  • excellent libraries
  • excellent IDEs, will detect syntax errors and many common coding errors before you build
  • strongly-typed language so that some common kinds of coding errors are simply impossible
  • easy to understand code for humans (depends on coding skills)
  • high performance is possible (depends on coding skills)
  • fast execution, almost as fast as the compiled languages like C++ and Go (Java is compiled to bytecode, the bytecode is then interpreted on the runtime platform which has built-in optimisers)
  • tidy installation of projects and their executable code, everything related to the project stays inside one folder path
  • the IDE can hot-load your code edits into running code (so you can edit your code and immediately see the results in a running code window)
  • can scale up to very large projects
  • never any need to worry about garbage collection, it's automatic
  • your completed app can be distributed as a single binary file (a .jar file)
  • if you learn Java, you have gained a lot of Javascript knowledge as well (totally different language but the syntax is the same)
  • Android apps are written in a super-set of Java
  • not tied to one specific vendor (you are not tied in to Microsoft)
  • if you don't like Java itself, there are other languages with different syntax which are essentially Java under the hood, examples are Scala and Groovy (in the same way that Visual Basic and C# are essentially the same language under the hood)

Disadvantages of Java

  • creating a GUI in Java is hard work, all the tools are a bit sucky
  • string processing in Java is good (plenty of library methods) but slow and memory-heavy in production code
  • multi-threading is well supported, but uses a lot of memory in production code (each thread has its own stack) - this problem is partially solved by reusable ThreadPools
  • there are plenty of Java libraries and platforms for web apps, but they all tend to be a bit bloated and heavy
  • running a mid-sized or large app needs 10 seconds or even longer on initial load
radfast
  • 538
  • 1
  • 7
  • 14