3 minor enhancements could greatly improve Java usability
The enhancements described below are fully backward compatible with existing java code.
1. Default package and class name for main() method
While Java requires that the 'entrypoint' point is called main()
it needs you to specify the class name containing that method in the MANIFEST.MF
file or on the command line.
If Java could pick a 'default' class name for the main method, eg app.Startup
this would reduce the need for all that extra config. It can still allow overriding the main class from the manifest or command line, but if it couldn't find anything there, it could just default to trying to execute the app.Startup.main()
method.
2. Java native dependency management
While Maven and Gradle allow you to do highly complicated builds, the vast majority of times that's not needed. If you are creating microservices, you really just need to:
- Download dependencies
- Compile source code
- Package everything into a Jar
There should be a Java native file format that's just a list of dependencies alongside the language version being used. Eg dependencies.txt
:
Java version 11
org.apache:commons-io:2.4.1
org.springframework:spring-data:4.2.1
com.mycompany:artifact:3.2.5
The java cli could just take this as input, download the dependencies in say ~/.java-deps
and compile your source code against it.
Go already does this with great success. It would mean that vast majority of developers, especially those new to Java, wouldnt need to learn about Maven or Gradle and all the complexities they bring.
It is so liberating to be free of the complexity of Maven/Gradle/NPM/Webpack.
— Prashant Deva (@pdeva) January 8, 2021
All one really needs to build a project is a text files with dependencies listed on each line. I just do a `go build` and it produces a binary. I dont need to read an entire book to build my project. pic.twitter.com/MySfY6p8jv
3. Self-contained jar support
Java still expects you to separately ship all the dependent libraries. Sure, there are plugins for Maven and Gradle to create an 'uber jar', and even Spring Boot does it out of the box. However, since this is how most people want to ship their java apps in a dockerized world it should be supported out of the box. Java should have a command-line option, say --uberjar
to build a self-contained jar containing all the dependencies.
Conclusion
With the above enhacements implemented, in a new java project you would just need to create:
app.Startup
class with amain()
methoddependencies.txt
file listing your dependencies
Now you can just write your code and use the Java CLI like to create a distributable jar:
java --uberjar dependencies.txt --source src myapp.jar
That's it! You won't need to learn about Maven/Gradle or any other config option to get started with Java.
One can only hope...