• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Getting java "System.out.println" content in a CF template

Community Beginner ,
Jul 03, 2007 Jul 03, 2007

Copy link to clipboard

Copied

All,
I'm a newbie to CF/JAVA integration and inherited about 20 java applications that are being called from a CF template. Smattered throughout the java code (hundreds of places) there is System.out.println() statements. So far I haven't been able to get this output back to display on the web page.

CF Code:

<CFSET UploadName = UploadsDir & "\" & cffile.serverFile>
<CFSET ExceptionName = UploadsDir & "\" & ExPrefix & BaseName>
<cfobject type="java" name="UploadObject" class="UploadProgram" action="create">
<cfset params = ArrayNew(1)>
<cfset params[1] = UploadName>
<cfset params[2] = ExceptionName>
#UploadObject.main(params)#

Java snippet:

System.out.println("");
System.out.println(recCount+" input records");
System.out.println(hdrCount+" header");
System.out.println(accCount+" good");
System.out.println(errCount+" bad");
System.out.println(ignCount+" ignored");
System.out.println("");
System.out.flush();

All help will be appreciated. Thanks!

TOPICS
Advanced techniques

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Beginner , Jul 03, 2007 Jul 03, 2007
Got it!

I finally figured out how to redirect the output stream and then return the var's .toString().

Java:

public static String main(String[] args)
throws Exception
{
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
PrintStream ps = new PrintStream( outstream );
System.setOut( ps );

if ( args.length < 1 )
{
System.out.println("usage: java InetLoad inet_sales_spreadsheet");
return(outstream.toString());
}
inetLoad.run();
return(outstream.toString());
}

CF:

<cf...

Votes

Translate

Translate
Guide ,
Jul 03, 2007 Jul 03, 2007

Copy link to clipboard

Copied

I can think of a few options. Truthfully, I think it would be easier in the long run if you rework the java method(s) to return the values you need so you can easily use them in ColdFusion. They can return a simple value like an int egeror a structure type object (ie. multiple values)

Another simple option is to run the clases using the command line java.exe program (using cfexecute). Then save the output to a file and parse it.


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 03, 2007 Jul 03, 2007

Copy link to clipboard

Copied

Thanks for the tip, however the printlns are everywhere. I was hoping that I wouldn't have to modify the java code in case another app was calling them. The methods are called from within a html wrapper and the raw output dumped into a DIV.

I'll give the cfexecute a shot again; I had tried this a few hours ago with no luck.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 03, 2007 Jul 03, 2007

Copy link to clipboard

Copied

You can also add a new method, if you're concerned about changing existing methods.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 03, 2007 Jul 03, 2007

Copy link to clipboard

Copied

Still banging my head against a wall. After doing some checking, I think I'm the only one using these java apps, but still the "println" is in hundreds of locations. Is there a way to redirect the output or feed it back to the calling CF template? I've tried CFOBJECT and CFEXECUTE and am getting nowhere. I've also tried using System.setOut(), but don't know what I'm doing.

Please keep in mind, that I'm fresh to java, so I could use code samples that I could cut/paste with little modification. Running these apps from the commandline gives me the proper output, I just can't get that same output back into my web page. If the printlns were everywhere, I could return the strings (maybe?). Lastly, I'm warry that if I write the strings out to file, that these apps are run asych so I'd have to design a filing scheme or risk overwrite.

Thanks for your patience with a frassled newbie.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 03, 2007 Jul 03, 2007

Copy link to clipboard

Copied

Got it!

I finally figured out how to redirect the output stream and then return the var's .toString().

Java:

public static String main(String[] args)
throws Exception
{
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
PrintStream ps = new PrintStream( outstream );
System.setOut( ps );

if ( args.length < 1 )
{
System.out.println("usage: java InetLoad inet_sales_spreadsheet");
return(outstream.toString());
}
inetLoad.run();
return(outstream.toString());
}

CF:

<cfoutput>
#UploadObject.main(params)#
</cfoutput>

Time for a beer.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 03, 2007 Jul 03, 2007

Copy link to clipboard

Copied

See the answer I gave you here I think you will find it helpful:-

http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=3&threadid=1281576&forumid=1

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 03, 2007 Jul 03, 2007

Copy link to clipboard

Copied

LATEST
Glad you figured it out. I probably still lean towards returning values from the method(s) instead of using System.out.println 😉 .. but since you said there are 100's of output lines, redirecting the output sounds like a good choice for now.

> Time for a beer.
Have two. You earned it 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation