Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Sending commands through Java to Terminal

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sending commands through Java to Terminal

    Hello,
    I am creating a .jar that will compile and run java programs through terminal. I have gotten the command to compile the java file that filePath refers to, but cannot figure out how to pass commands or text to an open Terminal.app window.
    I am using a mac.
    How I got my terminal window to open is by doing this:
    public static void main(String[] args) {
    		Desktop d = Desktop.getDesktop();
    		File f = new File("/Applications/Utilities/Terminal.app");
    		try {
    			d.open(f);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
     
    	}
    How do I pass code to the open window.
    I have tried using process builders and runtime execs but none of those would open the terminal window. I need the window open in case the user's .class file uses System.out and System.in commands so that they can interact with it.


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sending commands through Java to Terminal

    bump

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,304
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Sending commands through Java to Terminal

    I am using a mac.
    Are you asking how to tell the mac OS to open a terminal window? Have you asked on a mac forum?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Sending commands through Java to Terminal

    Why bother opening a terminal? Why not just sent the commands using the exec function of Runtime? Or a more advanced approach perhaps.

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sending commands through Java to Terminal

    Quote Originally Posted by Norm View Post
    Are you asking how to tell the mac OS to open a terminal window? Have you asked on a mac forum?
    The above code already opens a terminal window on mac, I am trying to send commands to it.

    Quote Originally Posted by Freaky Chris View Post
    Why bother opening a terminal? Why not just sent the commands using the exec function of Runtime? Or a more advanced approach perhaps.
    Because I need to use the "java [path]"command. The goal of this java project is to have the user select a file and then the class will run through a series of commands that determine the type of file, will compile it if necessary, and then pass it to the terminal to run the file.

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Sending commands through Java to Terminal

    But my point is that you can call "java [path]" from Runtime.exec or a more advanced method. Rather than needing to launch a terminal and the send commands to the terminal.
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sending commands through Java to Terminal

    Quote Originally Posted by Freaky Chris View Post
    But my point is that you can call "java [path]" from Runtime.exec or a more advanced method. Rather than needing to launch a terminal and the send commands to the terminal.
    But if the user has a file that sends and receives from the console, how are they to be able to see that if they don't have a terminal window. I have heard of sending the console to a text view or text area but I haven't found any working code.

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Sending commands through Java to Terminal

    Please look at the links below, the first is to Java 7 Runtime, which includes the exec function. This returns a Prcoess object (shown in the second link) from this you can retrieve input and output streams from the application and do as you like with them, i.e. forward them to stdio, or print them in a JTextArea or whatever you like.

    Runtime (Java Platform SE 7 )
    Process (Java Platform SE 7 )


    Regards,

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. how to test arrays in a terminal in java?
    By hemla in forum Java Theory & Questions
    Replies: 5
    Last Post: April 11th, 2013, 07:55 AM
  2. Integrated Java With Terminal
    By MFD-Stark in forum Java Theory & Questions
    Replies: 2
    Last Post: January 2nd, 2013, 12:23 PM
  3. [SOLVED] Stopping java in terminal
    By Scotty in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2011, 07:53 PM
  4. Need some help with OSX Java (Terminal)
    By mkoop in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 14th, 2011, 12:23 PM
  5. Executing Linux Commands with Java GUI
    By linuxrockers in forum AWT / Java Swing
    Replies: 2
    Last Post: February 15th, 2010, 10:57 PM