Results 1 to 6 of 6
  1. #1
    mike_ledis is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default System call with java

    Hello there!I am using ubuntu linux o/s. I want to make a program that can execute other programs via system calls. Is there any way to accomplish that with java?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    I've always used the ProcessBuilder class to run programs. It allows you to run a program (including any arguments). Then, you can optionally wait for the process to end.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CodesAway View Post
    I've always used the ProcessBuilder class to run programs. It allows you to run a program (including any arguments). Then, you can optionally wait for the process to end.
    Yep -- this is definitely a better answer than mine. Thanks for educating both the OP and me!

  5. #5
    farnabaz's Avatar
    farnabaz is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    I've always used the ProcessBuilder class to run programs. It allows you to run a program (including any arguments). Then, you can optionally wait for the process to end.
    would you give an example?

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Sure. Just know that I'm on a windows machine, so if you are not, this won't work (you'll need to change the program to something else) - the programs available (and their paths) aren't system independent.

    This is a simple example which opens a file in notepad (by passing a pathname as an argument). If you passed no arguments, it would open notepad, with an empty file.

    Java Code:
    import java.io.IOException;
    
    public class RunNotepad
    {
    	/**
    	 * Main method
    	 * 
    	 * @param args
    	 *            (not used)
    	 */
    	public static void main(String[] args)
    	{
    		// File to open in notepad
    		String pathname = "c:\\test\\test.txt";
    
    		/*
    		 * No path is necessary, since notepad is part of the PATH (by default)
    		 * (any program that is in a folder, which is part of the Windows PATH
    		 * environment variable can be started without specifying the directory)
    		 * 
    		 * You can pass arguments as parameters as well. For example, passing
    		 * a filename will open that file in notepad.
    		 * 
    		 * Note that you don't need to surround an argument in quotes, even if
    		 * it contains a space.
    		 */
    		ProcessBuilder notepad = new ProcessBuilder("notepad", pathname);
    
    		try {
    			// Start notepad
    			Process process = notepad.start();
    
    			// Wait for notepad to be closed
    			process.waitFor();
    
    			System.out.println("Congratulations you just started and closed notepad.");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    Last edited by CodesAway; 03-25-2010 at 11:39 PM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. how to call Java Bean Ireport frm Java application
    By Prashant.surwade in forum Advanced Java
    Replies: 8
    Last Post: 05-06-2012, 03:39 PM
  2. Replies: 3
    Last Post: 02-07-2010, 08:34 PM
  3. how to call dll from java ??
    By Omarero in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 05:14 AM
  4. how to call a dll from java
    By katie in forum Advanced Java
    Replies: 3
    Last Post: 12-10-2007, 10:34 AM
  5. help with System.exit (1) function call
    By katie in forum Advanced Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •