How to Compile and Run Java Program from Command Line
We can learn Java much easily by using command line to compile and test your examples.Once we know the Java syntax very well, we can switch to an integrated development environment(IDE) like Intellij IDEA, Eclipse etc.An IDE will save you time in coding but for the certification exam,our goal is to know details about the language and not have the IDE hide them for you. Let’s start now…
Firstly we should create to text file anywhere. For example I created text file in my desktop(Cat.txt)
Secondly we should write our codes.For example I writed this codes
public class Cat{
public static void main(String[] args){
System.out.println("Cat Name : "+args[0]);
System.out.println("Dog Name : "+args[1]); Dog dog = new Dog();
dog.doSmileToCat(args[0],args[1]);
}
}class Dog{
public void doSmileToCat(String nameCat,String nameDog){
if(nameCat == null || nameDog == null){
System.out.println("Where is the animal ?,I cant see");
} System.out.println("dog "+ nameDog +" smile to cat "+ nameCat);
}
}
Thirdly we must do file text name with public class name otherwise we take the error.Then we must change file extention with .java
Fourthly we should open the command line window and we should go to project file’s location
Then we should compile our codes.We use javac command for our codes convert bytecode format.
javac <file_name>.java
Thus we created our .class file.Then we should run the our java app.Firstly we should give our bytecode file to JVM therefore we should write java command
java Cat dobby snow
Well, we can execute our java app…