DEV Community

Cover image for The simple and satisfiying art of executing a java file from the command line.
Thinking out code
Thinking out code

Posted on

The simple and satisfiying art of executing a java file from the command line.

Having the next java source

public class Salad {

    public static void main(String[] args) {
        System.out.println("I'm the best salad in this neighberhood!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Run the command below, inside the folder source with java command:

java Salad.java
#it prints
I'm the best salad in this neighberhood!
Enter fullscreen mode Exit fullscreen mode

Now let’s create another Java file in the same folder.

public class Watermelon {

    public void talk() {
        System.out.println("I'm the best fruit!");
    }
}
Enter fullscreen mode Exit fullscreen mode

and call its method talk in Salad class contained in Salad file.

public class Salad {

    public static Watermelon watermelon = new Watermelon();

    public static void main(String[] args) {
        watermelon.talk();//here
        System.out.println("I'm the best salad in this neighberhood!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Let’s run the previous command once again.

java Salad.java
#it prints
Salad.java:3: error: cannot find symbol
        public static Watermelon watermelon = new Watermelon();
                      ^
  symbol:   class Watermelon
  location: class Salad
.\Salad.java:3: error: cannot find symbol
        public static Watermelon watermelon = new Watermelon();
                                                  ^
  symbol:   class Watermelon
  location: class Salad
2 errors
error: compilation failed
Enter fullscreen mode Exit fullscreen mode

Because our Salad class doesn’t know any about the Watermelon class, thought the Watermelon class contained inside the Watermelon file knows how to talk, so let’s add some watermelon to our salad.

At first, is neccesary to process our Watermelon file  into our processedFruits folder with the javac command and -d option to indicate folder location name.

javac -d processedFruits Watermelon.java
Enter fullscreen mode Exit fullscreen mode

And finally, let’s add our processedFruits folder to the original run command adding the -cp option to indicate where are compiled our java source of Watermelon file.

java -cp "processedFruits\" Salad.java
#it prints
I'm the best fruit!
I'm the best salad in this neighberhood!
Enter fullscreen mode Exit fullscreen mode

That’s it, simple but effective:

  • Run a standalone java file.
  • Run a standalone java file adding compiled sources as need it.

Command and options dictionary:

  • java run .class and .java files.
  • javaccompile .java files and creates .class files.
  • -dindicate output folder.
  • -cpindicate compiled folders directory(classpath).

Tech stack

  • Java 11.
  • Windows 10.

Repo

Top comments (0)