Batakj
Batakj

Reputation: 12763

Swing Application prevents to write a file

1st Application

Swing Client : This client take A URI as input. And The provided URI is the URL of a webservice. On submission, the client execute the WebService Client with the URI as parameter.

For executing the webservice client,

String command = "java -jar EncryptToken.jar " + requestURI;
Runtime.getRuntime().exec(command);

As the jar is provided by the client and have no public method except main method which accept requestURI.

2nd Application

WebService Client : This is the webservice client which is used to call webservice hosted in a server.

In the decompiled code, i found the below code for logging the Console output into the logger file.

    OutputStream outStream = System.out;   
    try{
        OutputStream os = new FileOutputStream("EncryptToken.log", false); // only the file output stream  
        TeeOutputStream tos = new TeeOutputStream(outStream, os); // create a TeeOutputStream that duplicates data to outStream and os  
        PrintStream fileStream = new PrintStream(tos);  
        System.setErr(fileStream);     
        System.setOut(fileStream);  
    }catch(FileNotFoundException ex){
        out.println("[Golden Client] Log file create Exception = " + ex.toString());
        return false;
    }

Question

When I execute the command java -jar EncryptToken.jar http://example.org/GCWS/YesWS through terminal or windows command line.

The command execute successfully and console output is written into the file and the size of file varies between 70-90 KB.

But, when i try to execute the same with Swing Client, Command is executed but the log file first initialize with 1KB, then it goes to 5KB and then freezed. The 5 KB log contains unterminated xml. When i close the swing client, then the rest of the console output is appended to log file.

I am not reading or writing the file in Swing Client. There is no System.out or System.err statement in Swing Client.

I am not able to figure out the exect root cause. Please help.

Upvotes: 1

Views: 67

Answers (1)

Gee Bee
Gee Bee

Reputation: 1794

Ensure that all of the output streams and the file are closed.

These streams work as a buffer. If you write them it is not really written to the disk, unless:

  • the in-memory queue is full (like around a few kilobytes)
  • you call flush() on a stream to force writing it out
  • you close the stream (which also forces writing out all pending data)

When the JVM is stopped, it tries to close all stuffs, that's why it seems working in the command line. When starting from a swing app, the JVM is not stopped until you stop the swing app, therefore it seems that some part of the file is not written to the disk.

Upvotes: 0

Related Questions