0

I've been studying Java and I wanted to make an application that will open other applications like terminal on the mac.

I have failed multiple times on getting it to work and I am a bit confused at this point.

Here is my code

package pac;



import java.awt.Desktop;

import java.io.File;

import java.io.IOException;

import javax.swing.*;

public class VnET extends JFrame {


    public VnET() {
    JFrame frame = new JFrame("Frame");

    //Optional: What happens when the frame closes?
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create components and put them in the frame.
    frame.setTitle("VnET");

    // Size the frame.
    frame.setSize(500, 250);;

    // Show it.
    frame.setVisible(true);
    //centers window
    frame.setLocationRelativeTo(null);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run()   {
                new VnET();
            Runtime r = Runtime.getRuntime();
            Process p = null;
            try {
                p = r.exec("Terminal");
                p.waitFor();
            } catch (Exception e) {
                System.out.println("Error executing terminal");
            }
            System.out.println("Terminal returned"+ p.exitValue());
            }
        });
    }

}

And this is the console log

  Error executing terminal
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at pac.VnET$1.run(VnET.java:43)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Isaac
  • 1
  • 1

1 Answers1

0

This is not the correct way to open Terminal on a Mac.

p = r.exec("Terminal");

On a Mac, if you want to open an app through the command line (incidentally Terminal!), you can use this command:

open -a Safari

So, in your Java program, use the following line of code:

p = r.exec("open -a Terminal");

The open -a command tells the Runtime to open the "App" called "Terminal".

Refer this page for more info on the open command.

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31