- How To Compile Java Source Code with Javac Tool?
- Syntax
- Show Javac Version
- Compile Java Source Code
- Compile Multiple Java Files
- Compile All Java Files
- Print Detailed Compile Log In Verbose Mode
- Specify Java Files and Options From File
- Specify The Destination Path
- Specify Class Path
- Specify The Module or Library Path
- Compiling java files in all subfolders? [duplicate]
- 6 Answers 6
- How to compile multiple Java files when there are Java files in other packages?
- 3 Answers 3
- How to compile a .java file on Ubuntu?
- 5 Answers 5
- Including all the jars in a directory within the Java classpath
- 25 Answers 25
How To Compile Java Source Code with Javac Tool?
javac is a tool used to compile Java applications or source code. javac reads class and interface definitions and compiles them into bytecode class files. These class files have *.class extension.
Syntax
javac usage syntax is a bit complex according to generally Linux tools.
- OPTIONS are used to specify command-line options provided by javac
- SOURCEFILES are used to specify one or more source files to be compiled
- CLASSES are used to specify one or more classes to be processed for annotations such as YourPackage.YourClass
- ARGFILES are used to specify one or more files that contain options and sources files
Show Javac Version
Java Development Kit(JDK) and Java Runtime Environment(JRE) can have different versions we target this will also affect the version of the javac. If we want to use specific Java programming language version we need to check the javac version with the —version .
Compile Java Source Code
We will start with a simple javac example where we will compile a single Java source code file. As we know Java source code files generally use *.java extension. We will just provide the file name to the javac compiler which is my.java in this example.
We will use the following sample HelloWorld.java source code file. We will try to compile this Java source file with the javac command line tool.
Below we will provides the java source file name HelloWorld.java which will compile and create the compiled byte code file named HelloWorld.class . As you can see the new extension is .class which is a compiled Java class file.
Compile Multiple Java Files
In the previous example, we have compiled a single Java file. But this is insufficient for big projects where we need to compile multiple Java files in a single command. We can specify multiple Java files to the javac in order to compile to the byte code. We will just append the file names by separating them with spaces.
Compile All Java Files
We can also compile all java files in the specified directory we will just use asterisk and .java to specify files with java extension in the current directory.
We can also specify a different directory from the current working directory. In this example, we will compile all Java files residing in /home/ismail/MyProject .
Print Detailed Compile Log In Verbose Mode
During compilation, we may need to print detailed information like libraries, operations warnings etc. We can use the -verbose option which will provide detailed debug information about the compile operation.
Specify Java Files and Options From File
Up to now, we have provided the Java files and options from the interactive shell or command line. If there are a lot of detailed file names and options where we will run them over and over again we can use files to specify the Java file named and options. We will use a file named config which will provide Java files and javac option to the javac command. We will use @ sign before the config file.
Here is the config file content which is used in previous examples.
We will provide this config file with the @ sign like below. Keep in mind that we can use a different name than config for this feature.
Specify The Destination Path
After given Java files are compiled into byte code the new files are stored in the current working directory. These files will have the same name as the source file and extension will be class. We can specify different output or destination path to put created byte code or binary files We will use -d option and the destination path.
Specify Class Path
If we need to provide types required to compile our source file we can use the -cp option and provide classpath like below. If this option is not provided the CLASSPATH environment variable will be used.
Specify The Module or Library Path
We can also use -p option in order to specify modules location. But this option can be used after Java 9.
Источник
Compiling java files in all subfolders? [duplicate]
How to compile all java files in all subfolders on Unix, using javac?
6 Answers 6
Create a batch file:
. then execute it in the top-level source folder.
Both answers taken from this thread.
But as others suggested, a build tool would probably prove helpful.
Use a build tool such as Ant or Maven. Both lets you manage dependencies in a much better way than can be accomplished using e.g. the find UNIX tool. Both And and Maven also lets you define custom tasks to be performed in addition to compilation. Maven furthermore comes with conventions for managing external dependencies in remote repositories, as well as conventions for running unit tests and features that support continuous integration.
Even if you just need to compile your source files once in a while, you’ll probably find that setting up a simple Ant build.xml file can be a big time saver in the end.
Finally, most of the popular IDE and code editor applications has some kind of integration with Ant build scripts, so you can run all the Ant tasks from within the editor. NetBeans, Eclipse, IDEA and more also has built-in support for Maven.
Read this first, if you’re new to Ant. Below is the example build file from the link:
Once you’re familiar with Ant, you’ll find it easier to move to Maven.
Источник
How to compile multiple Java files when there are Java files in other packages?
I am compiling multiple files in a directory ( javac *.java ) but I have a problem when I try to do this.
I get compile errors saying that javac cannot find a symbol of an object.
I have multiple packages that contain Java files that are needed to run the main program. But it seems that trying to compile these one by one won’t work. It runs fine in my IDE but I’m interested in learning how it’s done via command prompt.
The main program is in the drivers folder. I have tried compiling the files in order of dependency but that didn’t work out.
3 Answers 3
Javac documentation provides all the necessary information. However, it might be useful to use Ant or Maven for command line builds.
This page provides a good example of using first javac and then Ant for building a simple project.
Here is an example project and how can it be compiled with javac.
The tree structure of the project is this:
There are two special directories — build for containing compiled classes and src to contain source files (could be in different subdirectories — packages).
The following command compiles the whole project and puts the result into the build directory.
The -sourcepath src specifies directory src as the place where all the source can be found by the compiler. The -d build options tells the compiler where to place the compiled files.
Option src/**/*.java tells the compiler what files to actually compile. In this specific case it tells javac to look two levels down and pick all *.java at that level.
If there are *.java files at different levels than a list of files needs to be specified. For this, one could create such listing as an external file and pass this files as in input option for javac .
Here is how this could be done under Linux/Unix:
The above command creates file source.txt that contains full paths for the found *.java files. For this example it contains:
In order to compile the project with the list of source files flushed into source.txt , the following command can be used:
Please note that @source.txt specified at the end that tells the compiler where to look for a list of source files. Please also note that the -sourcepath option can be omitted.
Here is how the directory structure changed after running the above command.
As can be observed the build directory now contains the compiled class files in respective packages.
And, if you’d like to run it, assuming, for example, that Driver has method main , the following command executes the program.
Please note that file separator : (colon) is used under Unix, for Windows change it to ; (semicolon).
Источник
How to compile a .java file on Ubuntu?
How can I compile a .java file?
What programs will I need? If I need the Java JDK I will also need help installing that. I’m very new to Ubuntu, so any program that I need to install I will need a tutorial on how to install them.
5 Answers 5
To compile the file, open your terminal and type
To run the generated class file, use
But to do this you need to have the Java JDK installed in your computer. You can install it with the instructions in How do I install Java?.
OpenJDK works best for me. It’s simple and I have never faced any problem with it. Just follow these simple steps:
From Terminal install open jdk
Write a java program and save the file as filename.java
Now to compile use this command from the terminal
If everything works well then a new «filename.class» file should be created.
To run your program that you’ve just compiled type the command below in terminal:
You can use any text editor (like gedit) ,
replace the filename with watever name you want
you need to be on same directory as the «present working directory» (got by running pwd ) while running the command from terminal.
If for example your file is my_file.java :
However, it is a common convention to give classes and files the same name.
You need to install a JDK, Java Development Kit. Ubuntu contains a metapackage default-jdk, which depends on currently prefered JDK. Now it is openjdk-6-jdk.
To compile a Java file to runnable .class file you can run
It is the most simple use-case and mostly it doesn’t work because java classes mostly depends on other java classes placed in libraries.
So you would probably like to use some more sophisticated solutions. Most text editors supports Java syntax highlighting, for example jEdit, kate or vim, but they don’t solve your compilation issue.
You have another option — you can install a full featured Java IDE. Ubuntu comes with both main OpenSource Java IDEs — NetBeans and Eclipse.
Источник
Including all the jars in a directory within the Java classpath
Is there a way to include all the jar files within a directory in the classpath?
I’m trying java -classpath lib/*.jar:. my.package.Program and it is not able to find class files that are certainly in those jars. Do I need to add each jar file to the classpath separately?
25 Answers 25
Using Java 6 or later, the classpath option supports wildcards. Note the following:
- Use straight quotes ( » )
- Use * , not *.jar
Windows
Unix
This is similar to Windows, but uses : instead of ; . If you cannot use wildcards, bash allows the following syntax (where lib is the directory containing all the Java archive files):
(Note that using a classpath is incompatible with the -jar option. See also: Execute jar file with multiple classpath libraries from command prompt)
Understanding Wildcards
From the Classpath document:
Class path entries can contain the basename wildcard character * , which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR . For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.
A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo . The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo , or vice versa.
Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo , not in foo/bar , foo/baz , etc.
The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required then the JAR files can be enumerated explicitly in the class path.
Expansion of wildcards is done early, prior to the invocation of a program’s main method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory foo contains a.jar , b.jar , and c.jar , then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar , and that string would be the value of the system property java.class.path .
The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp ) command-line option. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.
Источник