Using Java with the Terminal

I prefer example vs classic academic lessons, but alternating both is a good idea. So, after some weeks, here’s what I found/wrote.

a. The Terminal
The default folder when accessing the Terminal is <my_name> folder.
image

Using “ls -l” (no quotes) in the Terminal, will return a list of that folder contents.

Now, we can add a new folder for .java documents:

mkdir java

Then, we have to set that folder as the working folder:
cd java

To be sure you are correct, you can issue a ls -l cmmand and get the list of items in the current foder.

b. Now, we need an example of java code that do something. What I love are images (or photos). Because we are using Java, I downloaded its logo from wikipedia and that is what we will use:

412 x 767 is the size of the logo (@72 dpi). I resized it to 360 pixels tall.

The code example to load the image came from internet (link is lost):

import java.awt.*;
import javax.swing.*;
  
public class DisplayAnImage
{
    public static void main(String[] args)
    {
        String fileName = "Java_Logo";
        ImageIcon icon = new ImageIcon(fileName);
        JLabel label = new JLabel(icon);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JScrollPane(label));
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}

Place the code in a text file, using DisplayAnImage.java as file name. The only thing I modiy n the above source is the image file name.

In the Terminal, type:
javac DisplayAnImage.java

A file named DisplayAnImage.class will be created.

If you want to run (“Hey, we are here to do that !”) that program, type:
java DisplayAnImage

or simply double click on its icon…

and you will be able to see the newly created program with the Java logo image insde the wndow. Of course, you will have to resize the image to get the full logo.

I wrote a simple “front-end” for Java using Xojo, but the shell does not allows me to do what I wanted to do (or I was not able to do that).

The contents of the Results TextArea shows the default working folder using the shell with Xojo (here is El Capitan, but it is the same with Big Sur).

The Commands Control is also a TextArea. Using the Tab key allow to select a line at a time; click in Execute PushButton send the selected cod to the Shell.

I hope you will enjoy this Topic.

BTW: tested on Xojo 2015r1/El Capitan and 2021r1.1/Big Sur.

1 Like