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

Thread: Problems with PrintWriter

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Problems with PrintWriter

    Hello all,

    This is my first time posting on the forums, but I'm sure I will be a regular poster pretty soon as I'm very much involved in Java Programming now.

    I'm new to Java, but not new to programming, so I was a bit suprised when I had this issue. I have also googled the problem and had a lot of hits, but nothing that has solved the issue.

    I'm trying to write to a text file, some lines of data. Its for an email conversion program. I'm using Rational Application Developer for WebSphere. When I run the code, the text file is empty. Here is the code:

    try {
     
    			PrintWriter out
    			   = new PrintWriter(new BufferedWriter(new FileWriter(strFileName)));
     
    		      EmailItem emailItem = emailConv.convertStreamToEmailItem
    		      (
    		    		  new FileInputStream("test.dxl"), 
    		    		  "test", 
    		    		  ".\\");
     
     
    		      out.println("FROM: " + emailItem.getFrom());
     
    		      out.println("TO: " + emailItem.getTo());
     
    		      out.println("CC: " + emailItem.getCc());
     
    		      out.println("BCC: " + emailItem.getBcc());
     
    		      out.println("SUBJECT: " + emailItem.getSubject());
     
    		      out.println("BODY: ### " + emailItem.getBodyHtml() + "###");
     
    		      out.close();
     
    } catch (Exception e) {
     
    			e.toString();
    		    e.printStackTrace();
     
     
    		}

    I have tried not using bufferwriter, I have also tried flushing, although close() does do a flush anyway, I'm completely at a loss, whatever I do, nothing is written to the text file.

    Any idea's? Thanks in advance,

    Darren


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problems with PrintWriter

    Maybe if the PrintWriter were defined outside the try block it would work.

    EmailItem emailItem = emailConv.convertStreamToEmailItem
    (
    new FileInputStream("test.dxl"),
    "test",
    ".\\");
    I've never heard of passing a new FileInputStream as a parameter.

    However, I think it's where you put the PrintWriter that's the problem.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with PrintWriter

    Thanks very much for your response.

    Here is the new code, outside of the try block, unfortunately, same problem. The file is created, but nothing inside of it.

    As far as the fileinputstream being a parameter, this is an IBM specific call, that I can't control

    public void Convert(String strFileName) throws IOException
    	{
    		emailConv = new EmailConverterService();
     
    		PrintWriter out
    		   = new PrintWriter(new BufferedWriter(new FileWriter(strFileName)));
     
    		try {
     
     
     
    		      EmailItem emailItem = emailConv.convertStreamToEmailItem
    		      (
    		    		  new FileInputStream("test.dxl"), 
    		    		  "test", 
    		    		  ".\\");
     
     
    		      out.println("FROM: " + emailItem.getFrom());
     
    		      out.println("TO: " + emailItem.getTo());
     
    		      out.println("CC: " + emailItem.getCc());
     
    		      out.println("BCC: " + emailItem.getBcc());
     
    		      out.println("SUBJECT: " + emailItem.getSubject());
     
    		      out.println("BODY: ### " + emailItem.getBodyHtml() + "###");
     
     
    		} catch (Exception e) {
     
    			e.toString();
    		    e.printStackTrace();
     
     
    		}
     
    		out.close();

    Thanks again for your help so far.

    Darren

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Problems with PrintWriter

    Any exceptions being thrown?

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with PrintWriter

    I'm not sure if this has anything to do with it, I assumed it was to do with the environment. I can't run the code inside of RAD, I have to build a runnable JAR, if I do run it inside of RAD, I get the following error:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: MSGConverter (Not found in java.library.path)
    	at java.lang.ClassLoader.loadLibraryWithPath(ClassLoader.java:1008)
    	at java.lang.ClassLoader.loadLibraryWithClassLoader(ClassLoader.java:972)
    	at java.lang.System.loadLibrary(System.java:470)
    	at com.ibm.icm.edc.emailconv.exch.MSGReader.<clinit>(MSGReader.java:24)
    	at java.lang.J9VMInternals.initializeImpl(Native Method)
    	at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
    	at com.ibm.icm.edc.emailconv.exch.MSGHandler.convertMSGtoEmailItem(MSGHandler.java:311)
    	at com.ibm.icm.edc.emailconv.EmailConverterService.parseEmailContent(EmailConverterService.java:902)
    	at com.ibm.icm.edc.emailconv.EmailConverterService.convertStreamToEmailItem(EmailConverterService.java:230)
    	at com.ibm.icm.edc.emailconv.EmailConverterService.convertStreamToEmailItem(EmailConverterService.java:131)
    	at neocol.converter.ConverterService.Convert(ConverterService.java:24)
    	at neocol.converter.TestConvert.main(TestConvert.java:14)

    And I have no idea why! The fact that the file is created and the temp files are created, suggests that everything is running ok?

    Is there a way to get the stacktrace from a runnable jar?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Problems with PrintWriter

    Quote Originally Posted by dbridle View Post
    And I have no idea why! The fact that the file is created and the temp files are created, suggests that everything is running ok?
    Nope...your file is first created, then the exception is thrown before anything is written. Not sure what you mean by 'run inside of rad',

  7. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Problems with PrintWriter

    Quote Originally Posted by javapenguin View Post
    Maybe if the PrintWriter were defined outside the try block it would work.

    EmailItem emailItem = emailConv.convertStreamToEmailItem
    (
    new FileInputStream("test.dxl"),
    "test",
    ".\\");
    I've never heard of passing a new FileInputStream as a parameter.

    However, I think it's where you put the PrintWriter that's the problem.
    Are you just guessing? This is helpful to nobody.

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with PrintWriter

    Quote Originally Posted by copeg View Post
    Nope...your file is first created, then the exception is thrown before anything is written. Not sure what you mean by 'run inside of rad',
    Hi Copeg,

    What I mean is, im in side of Rational Application Developer, and run it, so that I can see exceptions, system.out messages, console etc. I get the linker error there. If I export it as a runnable JAR, the program runs.

    Furthermore, I have no idea where the MSGConvert portion has come from, it does not seem to be anywhere in my code and there are no classes of the same name.

    A system.out.println call, I assume this can only be read while in debug mode, as a runnable JAR, is there a way to retrieve those messages?

    Thanks

    Darren

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Problems with PrintWriter

    Quote Originally Posted by dbridle View Post
    A system.out.println call, I assume this can only be read while in debug mode, as a runnable JAR, is there a way to retrieve those messages?
    Run the jar from the command line :
    java -jar yourjarhere.jar

    This should allow you to see exceptions and println statements. My initial guess is that you are missing a library somewhere on your classpath, in both environments...you suggest 'it runs' as a jar, but since you cannot see exceptions as you run the jar you do not know if it runs correctly

  10. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with PrintWriter

    It does look like an issue with the path.

    Here is the result of the java -jar:

    29-Oct-2010 15:40:09 com.ibm.icm.edc.emailconv.EmailConverterService loadProps
    INFO: Unable to load configuration settings. JNI logging will use the default se
    tting.
    29-Oct-2010 15:40:09 com.ibm.icm.edc.emailconv.EmailConverterService loadProps
    INFO: Unable to load configuration settings. JNI logging will use the default se
    tting.
    Exception in thread "main" java.lang.reflect.InvocationTargetException
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
    der.java:58)
    Caused by: java.lang.UnsatisfiedLinkError: no MSGConverter in java.library.path
            at java.lang.ClassLoader.loadLibrary(Unknown Source)
            at java.lang.Runtime.loadLibrary0(Unknown Source)
            at java.lang.System.loadLibrary(Unknown Source)
            at com.ibm.icm.edc.emailconv.exch.MSGReader.<clinit>(MSGReader.java:24)
            at com.ibm.icm.edc.emailconv.exch.MSGHandler.convertMSGtoEmailItem(MSGHa
    ndler.java:311)
            at com.ibm.icm.edc.emailconv.EmailConverterService.parseEmailContent(Ema
    ilConverterService.java:902)
            at com.ibm.icm.edc.emailconv.EmailConverterService.convertStreamToEmailI
    tem(EmailConverterService.java:230)
            at com.ibm.icm.edc.emailconv.EmailConverterService.convertStreamToEmailI
    tem(EmailConverterService.java:131)
            at neocol.converter.ConverterService.Convert(ConverterService.java:24)
            at neocol.converter.TestConvert.main(TestConvert.java:14)
            ... 5 more

    In my workspace, I have added 3 jar files, that contain the classes I need for the project. They are in the build path and the editor picks up the methods and properties just fine. So what am I missing?

    Thanks again!

    Darren

  11. #11
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with PrintWriter

    Solved!

    The issue was the file I was reading in had 0 bytes in it. For some reason this was masked in a link error *boggle*. Anyway, thanks for everybodies help!

    I'm not sure how to change this thread to solved?

Similar Threads

  1. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  2. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  3. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  4. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM