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

Thinking Out Code
2 min readMar 14, 2022

Having the next java source

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

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

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

Let’s run the previous command once again.

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.

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.

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

--

--

Thinking Out Code
Thinking Out Code

Written by Thinking Out Code

Software engineer sharing stories & ideas on performant coding development, dev tools’ effective use’s & software development’s life experiences.🧠&♥ involved.