Выполнение Среды Выполнения На Mac

Я изучал Java, и я хотел сделать приложение, которое откроет другие приложения, такие как terminal на mac.

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

Вот мой код

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());
            }
        });
    }

}

И это журнал консоли

  Error executing terminal
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at pac.VnET

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)

.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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:709)
at java.awt.EventQueue.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)

1 ответ

  1. Это неправильный способ открыть терминал на Mac.

    p = r.exec("Terminal");
    

    На Mac, Если вы хотите открыть приложение через командную строку (кстати терминал!), вы можете использовать эту команду:

    open -a Safari
    

    Итак, в вашей программе Java используйте следующую строку кода:

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

    open -aКоманда сообщает среде выполнения, чтобы открыть » приложение «под названием»Терминал».

    Дополнительные сведения о команде open см. На этой странице.

    Надеюсь, это поможет!