Results 1 to 8 of 8
  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Command line text

    In java, how can you use the command line parameter 'ipconfig > ipInfo.txt', and then to save the text file onto you USB key?
    Ive tried many ways, but can't get my head around it.

    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Command line text

    Are you asking how to read a file and write another file?

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Command line text

    Hm not exactly. If you go into command prompt and type in: ipconfig > ipInfo.txt, the output of the command is copied into a created file called 'ipInfo.txt'.

    My question is, how can you do that in java? (As far as i know it will be executed from the exec() method from the Runtime class)

    Thanks

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Command line text

    Yes the Process class and Runtime class can be used to execute a program.

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Command line text

    Yeah i know. Ive have a method that reads in an array of commands and executes them.
    The sample code is an array of commands that are passed in the method called execCmds(String[] commands).

    public static void main(String args[])
    {
    //List of commands
    String[] command = new String[5];
    command[0] = "cmd";
    command[1] = "/C";
    command[2] = "cd";
    command[3] = "f:\\Year 3\\Semester 6\\Team Project";
    command[4] = "ipconfig > ipInfo.txt";

    try
    {
    //Execute the commands from array
    service.execCmds(command);
    }
    //catch any exceptions
    catch(Exception e)
    {
    //do nothing
    }
    }

    But I get an error while it tried to run:

    The filename, directory name, or volume label syntax is incorrect.

    ----jGRASP wedge2: exit code for process is 0.
    ----jGRASP: operation complete.

    Thanks

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Command line text

    But I get an error
    Is that error written to the text file by the ipInfo command?

    What are the contents of the command[] array supposed to do?
    Why not use another version of the exec() method?
    Last edited by Norm; 03-14-2012 at 02:48 AM.

  7. #7
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Command line text

    No. The command did not even create the text file at all. I think I have the commands in a wrong order, although in not sure?
    Other commands work fine such as 'dir', 'cd', 'ipcinfig' etc. But not these set of commands/

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Command line text

    If you want to change the working directory the exec() method has an override to do that.

Similar Threads

  1. Command line text output
    By Eleeist in forum New To Java
    Replies: 5
    Last Post: 01-23-2012, 09:54 PM
  2. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  3. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  4. Replies: 3
    Last Post: 04-26-2010, 09:00 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

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