Compiling Java *.class Files with javac
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
We rely on other people’s code in our own work. Every day.
It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.
The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.
Lightrun is a new kind of debugger.
It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.
Learn more in this quick, 5-minute Lightrun tutorial:
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:
DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.
The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.
And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
We’re looking for a new Java technical editor to help review new articles for the site.
1. Overview
This tutorial will introduce the javac tool and describes how to use it to compile Java source files into class files.
We’ll get started with a short description of the javac command, then examine the tool in more depth by looking at its various options.
2. The javac Command
We can specify options and source files when executing the javac tool:
javac [options] [source-files]
Where [options] denotes the options controlling operations of the tool, and [source-files] indicates one or more source files to be compiled.
All options are indeed entirely optional. Source files can be directly specified as arguments to the javac command or kept in a referenced argument file as described later. Notice that source files should be arranged in a directory hierarchy corresponding to the fully qualified names of the types they contain.
Options of javac are categorized into three groups: standard, cross-compilation, and extra. In this article, we’ll focus on the standard and extra options.
The cross-compilation options are used for the less common use case of compiling type definitions against a JVM implementation different from the compiler’s environment and won’t be addressed.
3. Type Definition
Let’s start by introducing the class we’re going to use to demonstrate the javac options:
public class Data < ListtextList = new ArrayList(); public void addText(String text) < textList.add(text); >public List getTextList() < return this.textList; >>
The source code is placed in the file com/baeldung/javac/Data.java.
Note that we use *nix file separators in this article; on Windows machines, we must use the backslash (‘\’) instead of the forward slash (‘/’).
4. Standard Options
One of the most commonly used standard options of the javac command is -d, specifying the destination directory for generated class files. If a type isn’t part of the default package, a directory structure reflecting the package’s name is created to keep the class file of that type.
Let’s execute the following command in the directory containing the structure provided in the previous section:
javac -d javac-target com/baeldung/javac/Data.java
The javac compiler will generate the class file javac-target/com/baeldung/javac/Data.class. Note that on some systems, javac doesn’t automatically create the target directory, which is javac-target in this case. Therefore, we may need to do so manually.
Here are a couple of other frequently used options:
- -cp (or -classpath, –class-path) – specifies where types required to compile our source files can be found. If this option is missing and the CLASSPATH environment variable isn’t set, the current working directory is used instead (as was the case in the example above).
- -p (or –module-path) – indicates the location of necessary application modules. This option is only applicable to Java 9 and above – please refer to this tutorial for a guide to the Java 9 module system.
If we want to know what’s going on during a compilation process, e.g. which classes are loaded and which are compiled, we can apply the -verbose option.
The last standard option we’ll cover is the argument file. Instead of passing arguments directly to the javac tool, we can store them in argument files. The names of those files, prefixed with the ‘@‘ character, are then used as command arguments.
When the javac command encounters an argument starting with ‘@‘, it interprets the following characters as the path to a file and expands the file’s content into an argument list. Spaces and newline characters can be used to separate arguments included in such an argument file.
Let’s assume we have two files, named options, and types, in the javac-args directory with the following content:
We can compile the Data type like before with detail messages printed on the console by executing this command:
javac @javac-args/options @javac-args/types
Rather than keeping arguments in separate files, we can also store them all in a single file.
Suppose there is a file named arguments in the javac-args directory:
-d javac-target -verbose com/baeldung/javac/Data.java
Let’s feed this file to javac to achieve the same result as with the two separate files before:
Notice the options we’ve gone through in this section are the most common ones only. For a complete list of standard javac options, check out this reference.
5. Extra Options
Extra options of javac are non-standard options, which are specific to the current compiler implementation and may be changed in the future. As such, we won’t go over these options in detail.
However, there is an option that’s very useful and worth mentioning, -Xlint. For a full description of the other javac extra options, follow this link.
The -Xlint option allows us to enable warnings during compilation. There are two ways to specify this option on the command line:
- -Xlint – triggers all recommended warnings
- -Xlint:key[,key]* – enables specific warnings
Here are some of the handiest -Xlint keys:
- rawtypes – warns about the use of raw types
- unchecked – warns about unchecked operations
- static – warns about the access to a static member from an instance member
- cast – warns about unnecessary casts
- serial – warns about serializable classes not having a serialversionUID
- fallthrough – warns about the falling through in a switch statement
Now, create a file named xlint-ops in the javac-args directory with the following content:
-d javac-target -Xlint:rawtypes,unchecked com/baeldung/javac/Data.java
When running this command:
we should see the rawtypes and unchecked warnings:
com/baeldung/javac/Data.java:7: warning: [rawtypes] found raw type: ArrayList List textList = new ArrayList(); ^ missing type arguments for generic class ArrayList where E is a type-variable: E extends Object declared in class ArrayList com/baeldung/javac/Data.java:7: warning: [unchecked] unchecked conversion List textList = new ArrayList(); ^ required: List found: ArrayList .
6. Conclusion
This tutorial walked through the javac tool, showing how to use options to manage the typical compilation process.
In reality, we usually compile a program using an IDE or a build tool rather than directly relying on javac. However, a solid understanding of this tool will allow us to customize the compilation in advanced use cases.
As always, the source code for this tutorial can be found over on GitHub.
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes: