How to compile Java with VsCode & Ant


Learn deeply about the Java build mechanism, learn build tools, and build a lightweight and simple learning environment without Eclipse. Target audience: People who understand Java but are not good at environment construction and deployment, and people who want to mention because their workplace uses Ant. By the way, this is me. My environment: macOS Assumption: The path to the Java VM is in place and the javac command works.

Install Ant

You can download it at Ant Official. スクリーンショット 2020-02-03 22.43.13.png Download the latest in zip format from here.

You can leave it as it is, but you can change it to a shorter name because it will be described in the environment variable later. It doesn't matter where you put it, but I put it in my home directory. I chose apache-ant here.

mv apache-ant ~/

Pass through

vim .bash_profile

Will open the path setting file, so (type the CD command with no arguments to go to the HOME directory)

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/library/java/JavaVirtualMachines/jdk-12.0.1.jdk/contents/home/bin:$ANT_HOME
if [ -f ~/.bashrc ]; then
  . ~/.bashrc

Add a path like the one above. ANT_HOME=~apache-ant/bin :$ANT_HOME Has just been added. Please note that other commands are different depending on the person, so if you copy and paste this whole, other commands may be broken.

ant --version

If there is a response, the installation is successful.

Prepare the sauce

Directory structure

スクリーンショット 2020-02-03 22.58.31.png

Put the build artifact in bin and the source code in src. ・ ・ Describe that in build.xml.


<?xml version="1.0" encoding="UTF-8" ?>
<project name="HelloAnt" default="default" basedir=".">
  <target name="default">
    <javac srcdir="./src/" destdir="./bin/"/>
Is magic. Maybe if you don't write this, the characters will be garbled. Until you get used to it

I think it's like an HTML DOCTYPE declaration that looks good every time.

I think that it is one eclipse project by wrapping \ . Write specific support in \ . It seems that it will be dependent or this name. \ A path that can be specified in the javac command. The bin and src from earlier are specified here. In other words, if you change this, you can build wherever you like. Normally, I would write bin and src.

class Main{

  public static void main(String args[]){
    System.out.println("hello ant");

Place it in the src directory and you're ready to go.


Just go to the directory where build.xml is and type the command below.


BUILD SUCCESSFUL Is displayed, it is successful. You should have Main.class in your bin directory. スクリーンショット 2020-02-03 23.15.46.png

Recommended Posts

How to compile Java with VsCode & Ant
[Java] How to compare with equals method
How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java to play with Function
Java --How to make JTable
[Java] How to omit spring constructor injection with Lombok
How to deploy Java to AWS Lambda with Serverless Framework
How to use java Optional
How to minimize Java images
How to write java comments
[Java] How to encrypt with AES encryption with standard library
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to number (number) with html.erb
How to use Java Map
How to update with activerecord-import
How to set Java constants
Connect to DB with Java
Connect to MySQL 8 with Java
How to build Java development environment with VS Code
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
[Java] How to start a new line with StringBuilder
How to use trained model of tensorflow2.0 with Kotlin / Java
How to handle exceptions coolly with Java 8 Stream or Optional
[Easy] How to automatically format Ruby erb file with vsCode
Investigated how to call services with Watson SDK for Java
How to scroll horizontally with ScrollView
How to study Java Silver SE 8
How to use Java HttpClient (Get)
Java to learn with ramen [Part 1]
Studying Java # 6 (How to write blocks)
[Java] Points to note with Arrays.asList ()
How to run Ant in Gradle
[Java] How to update Java on Windows
How to make a Java container
How to disassemble Java class files
How to enclose any character with "~"
How to use Java HttpClient (Post)
[Java] How to use join method
Dare to challenge Kaggle with Java (1)
How to learn JAVA in 7 days
[Processing × Java] How to use variables
I tried to interact with Java
[Java] How to create a folder
How to use mssql-tools with alpine
How to decompile java class files
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to write Java variable declaration