Results 1 to 8 of 8
  1. #1
    clover is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default Passing arguments to jar file with a pipe

    Hello,
    I'm learning to use jar files. I'd like to run one with arguments passed from another program in a future but now I'm trying at least to echo them into it:
    Java Code:
    echo argument | java -jar program.jar
    It doesn't work (as if there were no arguments), although
    Java Code:
    java -jar program.jar argument
    works and I try it with the simplest program possible:
    Java Code:
    import java.io.*;
    
    public class Main {
    	public static void main(String[] args) {
    		System.out.print("arg: " + args[0]);
    	}
    }
    What's wrong?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Have you tried putting the java.exe call in a batch file?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    If you pipe some text to another process that text shows up in the process' stdin stream, i.e. it can be read by reading from System.in

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    as if there were no arguments
    There are no arguments. You pipe the output of your program to a variable called "argument", but that doesn't make it one.
    No bug ever had to calculate its fitnessfunction.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Yoohoo! Piping goes through the streams (notably stdout and stdin); read reply #3.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    clover is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Have you tried putting the java.exe call in a batch file?
    Ehrm, it is in my classpath, but I'm not sure what a batch file is...

    Quote Originally Posted by JosAH View Post
    If you pipe some text to another process that text shows up in the process' stdin stream, i.e. it can be read by reading from System.in
    Oh yes, that's it, thanks!

  7. #7
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Yoohoo
    Sorry, I didn't use piping for a long time. I thought to remember it worked right to left, like redirection "<".
    No bug ever had to calculate its fitnessfunction.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Jodokus View Post
    Sorry, I didn't use piping for a long time. I thought to remember it worked right to left, like redirection "<".
    Pipes and I/O redirection are a gift from the gods; they're so darn simple if you think of it and they only take a small buffer to implement ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Passing array arguments into main
    By JohnDas in forum New To Java
    Replies: 12
    Last Post: 11-10-2010, 03:00 PM
  2. Replies: 8
    Last Post: 09-23-2010, 12:07 AM
  3. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-07-2010, 11:30 PM
  4. Dynamic pipe
    By deschutron in forum Threads and Synchronization
    Replies: 3
    Last Post: 10-03-2009, 10:48 AM
  5. Replies: 1
    Last Post: 09-10-2009, 01:58 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
  •