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 5 of 5

Thread: Using external .exe for input stream

  1. #1
    Junior Member
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Using external .exe for input stream

    I have an external console app I am trying to read from. My code starts the .exe process. I am using a buffered reader, .getinputstream, and then .readLine. I then print the Line to the console. I have some unexpected results doing this. After starting the Process i do not see it in my task manager processes. I used isAlive() to verify that the process has actually started and, it returns true. If I run the process in a separate java file, the process starts correctly. I can see it in task manager processes.

    2)When running the code, I get intermittent results. Most of the time .println(Line) does not print anything(in the eclipse). I have seen the println work on this code a couple of times but, it is not constantly working.

    I have only been writing in java for about two weeks, so my understanding of how most of these functions work is remedial. I feel like this is either a timing issue between the .exe and my code or i am using something for the wrong purpose as well as making some things harder than they have to be. Any help would be greatly appreciated.

    Windows 10, Eclipse IDE

    !EXPECTED RESULTS!
    STALL WARNING = 0 // APP SENDS SIGNAL THAT STALL IS OFF
    STALL WARNING = 1 // APP SENDS SIGNAL THAT STALL IS ON ECT....
    STALL WARNING = 0
    STALL WARNING = 1

    !ACTUAL RESULTS!
    .... //ELAPSED TIME 5-20 MINUTES NO RESULTS
    STALL WARNING = 0
    .... //ELAPSED TIME - SEEMINGLY RANDOM
    STALL WARNING = 1 // RESULT WILL EITHER RETURN 0 OR 1 REGARDLESS OF THE VALUE THE APP SHOULD BE SENDING AT THAT TIME

    public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
     
     
     
    	try {	
    			 String line;
     
    			 InputStreamReader Isr = new InputStreamReader(Ignition().getInputStream());
     
    			BufferedReader input = new BufferedReader(Isr);
    			{
    			    while ((line = input.readLine()) != null) {
    				  line = input.readLine();
    				  System.out.println(line);
    				  line = input.readLine();
     
    			    	System.out.println(line);
    			    }			
     
    		         input.close();
    		    }
     
    		} catch (Exception e) {
    			e.printStackTrace();			
    		}
     
    	Ignition().destroy();
    	System.out.println("Program ended");
    	 }
     
    	public static Process Ignition() throws IOException{
     
     
    				 String appname = new String("TaggedData.exe");
    				 String Fname = new  String("C:\\Users...\"+ appname);
     
     
     
     
     
    		String command[] = {Fname};  
     
    		ProcessBuilder Prep =new ProcessBuilder(command);
     
    		Process P3d = Prep.start();
     
           return P3d ;
     
    	}
     }
    Last edited by SimJim21; August 10th, 2021 at 08:51 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,012
    Thanks
    63
    Thanked 2,705 Times in 2,655 Posts

    Default Re: Using external .exe for input stream

    I get intermittent results.
    Can you copy the contents of the console that was printed that shows what you are talking about?
    Add some comments in the print out where the results are not what you expect.

    In the code I see that three lines are read but only the last two lines are printed.
    The first line that is read is not printed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using external .exe for input stream

    Quote Originally Posted by Norm View Post
    In the code I see that three lines are read but only the last two lines are printed.
    The first line that is read is not printed.
    Does this mean that
    while ((line = input.readLine()) != null) is already reading the line into the "line" string?

    If so, am i possibly writing a 0 length string to "line"?
    that may be why i am not seeing my results.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,012
    Thanks
    63
    Thanked 2,705 Times in 2,655 Posts

    Default Re: Using external .exe for input stream

    while ((line = input.readLine()) != null) is already reading the line into the "line" string?
    Yes. The call to readLine returns the next String from input.

    why i am not seeing my results.
    The line that was read is not printed. Add a print statement immediately after the while to print line.

    I would expect the output to be this:
    skips line 1
    prints line 2
    prints line 3
    skips line 4
    prints line 5
    prints lint 6
    skips line 7
    etc...
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    SimJim21 (August 10th, 2021)

  6. #5
    Junior Member
    Join Date
    Aug 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using external .exe for input stream

    Quote Originally Posted by Norm View Post
    Yes. The call to readLine returns the next String from input.


    The line that was read is not printed. Add a print statement immediately after the while to print line.

    I would expect the output to be this:
    skips line 1
    prints line 2
    prints line 3
    skips line 4
    prints line 5
    prints lint 6
    skips line 7
    etc...
    Norm, You are the MAN!!! that was the problem. it was writing a zero length string over my string var. Instant updates now. As I said Java is very new to me. I didnt know checking the string like that would assign the value as well. Makes sense thought. Thanks a lot for answering such a noob question.

Similar Threads

  1. Input stream buffers up to 4K before available to read
    By sasatap in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 18th, 2014, 11:33 AM
  2. [SOLVED] reading a stream of numbers from standard input
    By mia_tech in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 25th, 2012, 12:17 AM
  3. could not create audio stream from input stream
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 2nd, 2011, 02:08 AM
  4. url input stream returning blank
    By yo99 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: June 2nd, 2010, 08:14 PM
  5. Client input Stream
    By gisler in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 19th, 2009, 09:30 PM