Java compiling more classes

Compile All Java Classes in Directory Structure with javac

announcement - icon

As always, the writeup is super practical and based on a simple application that can work with documents with a mix of encrypted and unencrypted fields.

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:

announcement - icon

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:

announcement - icon

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.

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In some special circumstances, we don’t have a third-party build tool installed, such as Ant or Maven. And, we still need to compile a project with lots of packages and classes.

In this tutorial, we’re going to use the javac command to accomplish this task with different scenarios.

2. Using File Names

Let’s assume we have two directories in the current directory: src and out. The src directory holds our Java source files, and the out directory will contain the corresponding compiled class files.

Let’s start with a simple scenario. The src directory contains a single Java source file named com/baeldung/MyClass.java:

2 javac compile all java source files in a directory structure 01

Then, let’s use javac to compile the MyClass.java file into the out directory:

$ javac -d ./out/ ./src/com/baeldung/MyClass.java

In the above command, the -d option specifies the destination directory for the class file. Also, we should note that the exact code of the MyClass.java file isn’t that important, and we only need to ensure it’s a grammar-correct Java file.

To be a little complicated, let’s add another three Java files – YourClass.java, HerClass.java, and HisClass.java:

javac compile all java source files in a directory structure 02

To compile all four of the above Java files, we can list each of them on the command line:

$ javac -d ./out/ \ ./src/com/baeldung/MyClass.java \ ./src/com/baeldung/YourClass.java \ ./src/com/baeldung/HerClass.java \ ./src/com/baeldung/HisClass.java

2 javac compile all java source files in a directory structure 03

Then, let’s add a new Main.java file that refers to the other four Java files, for example, by invoking methods or creating an object instance:

In this case, we only need to compile the Main.java file:

$ javac -sourcepath ./src/ -d ./out/ ./src/com/baeldung/Main.java

After the above command, the other four class files will also be compiled. That’s because javac will search for the required types and compile the corresponding source files by default. If we don’t want to compile the required types, we can add the -implicit:none option.

The -sourcepath option tells the Java compiler where to find the input source files. If the -sourcepath option isn’t specified, javac will utilize the user classpath to search for both the class files and source files. So, we can replace the -sourcepath option with the -classpath or -cp option:

$ javac -cp ./src/ -d ./out/ ./src/com/baeldung/Main.java

However, this approach has its limitations: the javac command only compiles the required types and omits other source files. For example, if we add a new ItsClass.java and the Main.java doesn’t refer to it, then ItsClass.java will not be compiled:

2 javac compile all java source files in a directory structure 04

To summarize, there are two scenarios suitable to list filenames in the javac command line: when there are just a few Java source files, and when there is a launcher class that refers to other classes recursively.

3. Using Wildcard

The javac command also supports the wildcard character (*) for compiling multiple source files in the same directory.

For example, we can use the wildcard to compile the above source files:

$ javac -d ./out/ ./src/com/baeldung/*.java

To complicate our scenario further, let’s add four sub-packages (spring, summer, autumn, and winter) and corresponding classes:

javac compile all java source files in a directory structure 05

Now, in the command line, we can list each package with a wildcard to compile all of them:

$ javac -d ./out/ \ ./src/com/baeldung/*.java \ ./src/com/baeldung/spring/*.java \ ./src/com/baeldung/summer/*.java \ ./src/com/baeldung/autumn/*.java \ ./src/com/baeldung/winter/*.java

When there are only a few packages, regardless of the source file number, it’s suitable to use this wildcard approach.

4. Using Argument Files

When there are multiple packages to compile, then using the javac command with an argument file comes in handy. An argument file can include both the javac options and source file names.

To use an argument file, we need to prepend the at sign (@) leading character before the argument filename:

But how can we generate such a @sources.txt file? That depends on the OS we’re using. In Linux or macOS, we can use the find command:

$ find ./src/ -type f -name "*.java" > sources.txt

In the above command line, the ./src/ is our search starting-point directory, the -type f option filters only regular files, and the -name “*.java” option matches all filenames with the .java extension.

However, in Windows, we can use the dir command:

> dir src /b /s *.java > sources.txt

In the above command line, the src folder is our search path, the /b switch shows the directory and file names without additional information, and the /s option lists all files in a specified directory and all subdirectories.

The shortcoming of this approach is that each time we add a new or remove an existing Java source file, we need to regenerate the sources.txt file.

5. Other Approaches

Besides the above common approaches, there also exist other OS-dependent approaches, such as using globstar or pipe.

5.1. Using Globstar

Bash version 4.0 adds a new globbing option called globstar that treats the double wildcard (**) differently. With it enabled, Bash will traverse multi-level directories; otherwise, Bash will only search a single-level directory.

However, this option is disabled by default. And, we can use the shopt (sh + opt, shell options) command to inspect the Bash option settings. If we execute the shopt command without any argument, it will output a long list of options and their statuses (on or off).

Currently, we’re only concerned with the globstar option:

$ shopt globstar globstar off

To enable it, we use the shopt command with the -s option:

To disable it, we invoke the shopt command with the -u option:

After enabling this option, we can invoke javac with the double wildcard:

5.2. Using a Pipe

Conceptually, a pipe is a connection between two processes. And, we can utilize this pipe mechanism to connect multiple commands to produce our desired results.

To compile our Java source files, we can combine the find, xargs, and javac commands:

$ find ./src/ -type f -name "*.java" | xargs javac -cp ./src/ -d ./out/

Also, the find command supports the -exec action:

$ find ./src/ -type f -name "*.java" -exec javac -cp ./src/ -d ./out/ '<>' ';'

The above command line may run a bit slowly. That’s because the javac command will run for each matched file. For more information, we can use the man find command to read the -exec option’s documentation.

To be a little faster, we can change the semicolon (;) into a plus sign (+). Then, the javac command will collect all the matched files and execute only once:

$ find ./src/ -type f -name "*.java" -exec javac -cp ./src/ -d ./out/ '<>' +

6. Conclusion

In this article, we first looked at some common approaches to compiling all Java source files in a directory structure, such as using filenames, wildcards, and an argument file. Then, we looked at some OS-dependent approaches, such as using globstar and pipe.

announcement - icon

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:

Источник

Can we declare more than one class in a single Java program?

A single Java program contains two or more classes, it is possible in two ways in Java.

Two Ways of Implementing Multiple Classes in a single Java Program

How the compiler behave with Multiple non-nested classes

In the below example, the java program contains two classes, one class name is Computer and another is Laptop. Both classes have their own constructors and a method. In the main method, we can create an object of two classes and call their methods.

Example

public class Computer < Computer() < System.out.println("Constructor of Computer class."); >void computer_method() < System.out.println("Power gone! Shut down your PC soon. "); >public static void main(String[] args) < Computer c = new Computer(); Laptop l = new Laptop(); c.computer_method(); l.laptop_method(); >> class Laptop < Laptop() < System.out.println("Constructor of Laptop class."); >void laptop_method() < System.out.println("99% Battery available."); >>

When we compile the above program, two .class files will be created which are Computer.class and Laptop.class. This has the advantage that we can reuse our .class file somewhere in other projects without compiling the code again. In short, the number of .class files created will be equal to the number of classes in the code. We can create as many classes as we want but writing many classes in a single file is not recommended as it makes code difficult to read rather we can create a single file for every class.

Output

Constructor of Computer class. Constructor of Laptop class. Power gone! Shut down your PC soon. 99% Battery available.

How the compiler behave with Nested classes

Once the main class is compiled which has several inner classes, the compiler generates separate .class files for each of the inner classes.

Example

// Main class public class Main < class Test1 < // Inner class Test1 >class Test2 < // Inner class Test2 >public static void main(String [] args) < new Object() < // Anonymous inner class 1 >; new Object() < // Anonymous inner class 2 >; System.out.println("Welcome to Tutorials Point"); > >

In the above program, we have a Main class that has four inner classes Test1, Test2, Anonymous inner class 1 and Anonymous inner class 2. Once we compile this class, it will generate the following class files.

Output

Welcome to Tutorials Point

Источник

Читайте также:  Настройка java на самсунг
Оцените статью