Solved

Intercepting Java application output

Posted on 2001-08-09
34
297 Views
Last Modified: 2010-03-31
I'm just wondering if ther is anyway of writing a software that will be able to java .class file and then intercepting all its output for manipulation and display the manipulated output on the screen in replace of the original ones.

for example if I compile a source file that have one of the line :

System.out.println("Testing");

and compile it as abc.class

if i run the abc.class, is there anyway of developing a program that can intercept the output and manipulate it from "Testing" to "TESTING" before displaying on the screen?

so that on the screen displayed:

TESTING

instead of the original "Testing"
0
Comment
Question by:jerntat
  • 12
  • 11
  • 11
34 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6371130
Certainly, you just need an app that reads from stdout.
0
 
LVL 92

Expert Comment

by:objects
ID: 6371132
Just need to pipe output to your other app:

java abc | myapp
0
 

Author Comment

by:jerntat
ID: 6371237
what if I'm using some GUI components, such as

Label = new JLanel("Testing");
text = new JTextField("Testing 2", 20);
button = new JButton("Testing 3");

and I want the look and feel of the GUI to be the same but changing only the text being displayed.
0
 
LVL 3

Accepted Solution

by:
dnoelpp earned 100 total points
ID: 6371311
This looks like an "aspect". Please see the aspect-oriented programming home page: http://aspectj.org and http://aspectj.org/documentation/primer/newcomers/aop.html

The only difficulty, IMO, would be to find the aspect you want to modify. First you said, just the writing to the standard output, but then your aspect is more complicated: all strings you define should be modified. Please experiment a little with AspectJ before you do this.

Another thing worth a look: BCEL. With this you can manipulate compiled java classes before loading. This can be exploited to modify little things in the JVM. An example, write profiling output in the beginning and ending of each method. Link: http://bcel.sourceforge.net

Cheers!
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6371313
In fact, as far as I know, there is a project using BCEL to implement aspects... :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 6371485
> and I want the look and feel of the GUI to be the same
> but changing only the text being displayed.

Thats an entirely different issue than the question (I thought) you asked.

DO you want to modify your class file, or manipulate stdout coming from your app?

0
 

Author Comment

by:jerntat
ID: 6371539
My main problem is that I have a application class file that would generate some sort of GUI and provide certain function and I wanted to change the text into another language in the GUI components but let the function of the application to remain the same.

I thought if there is a way to manipulate the stdout like System.out.println, I could also use the same method to manipulate the text used on GUI components


0
 
LVL 92

Expert Comment

by:objects
ID: 6371585
Different things really.

Easiest way would be to go thru the code replacing strings preferably with getProperty() calls so you can modify the text via property files.
Have a read up on i18n which is basically what you're trying to do. In fact I think Sun has a tool for going thru code looking for strings which may be exactly what you're after.

Good luck :)
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6371587
Use ResourceBundle.
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6371595
Read the javadoc about java.util.ResourceBundle. This class is exactly for this type of thing you need.
0
 

Author Comment

by:jerntat
ID: 6371596
but i dont have the source file...
0
 
LVL 92

Expert Comment

by:objects
ID: 6371602
Legal issues aside, you could simply decompile it.
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6371606
You have the documentation in the docs subdirectory, if you installed the api docs, and the source code is in src.jar. But this is a different question.
0
 

Author Comment

by:jerntat
ID: 6371622
I try not to decompile, doesnt seems right to me. Working on the bytecode seems to be a bit better alternative :)

technically still the same i suppose....
0
 

Author Comment

by:jerntat
ID: 6371626
i mean i dont have the source code to the application class file whoose output i wanted to alter.
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6371627
Why? Using ResourceBundle is the standard way of implementing multi-language applications. I wouldn't use BCEL or AspectJ to implement multi-language.
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6371628
Ah, I understand, you have an other application and you need to replace its strings...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 6371632
> I try not to decompile...

Good to hear.

> technically still the same i suppose....

Certainly is :)


0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6371635
You could use Mocha, a Java decompiler, replace the strings by hand and recompile your class again:

http://www.brouhaha.com/~eric/computers/mocha.html

There are other decompilers, too. Go to a good search machine and give "Java" and "Decompiler" as keywords.

If you need to change the strings during runtime, the best bet is to learn BCEL and use it.
0
 

Author Comment

by:jerntat
ID: 6371638
i mean i dont have the source code to the application class file whoose output i wanted to alter.
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6371655
I am sorry, I just discovered that I duplicated a post from objects, but this thread went a little too fast... (advice about decompiling the class)
0
 

Author Comment

by:jerntat
ID: 6371659
is ther any other way?...for example going really low level, by intercepting the windows messages and changing the text on the fly or probably build my own bytecode interpter so as to leave the application class alone.
0
 

Author Comment

by:jerntat
ID: 6371662
is ther any other way?...for example going really low level, by intercepting the windows messages and changing the text on the fly or probably build my own bytecode interpter so as to leave the application class alone.
0
 
LVL 92

Expert Comment

by:objects
ID: 6371671
> for example going really low level...

Re-engineering the entire app sounds easier :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 6371675
Have you considered asking the original author permission to i18n the code?
0
 
LVL 92

Expert Comment

by:objects
ID: 6371680
> I am sorry, I just discovered that I duplicated a post from objects

No worries, it happens.
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6371689
Maybe AspectJ does some of the work (own bytecode interpreter) to implement the aspects. Your aspect is "putting strings to Swing". This aspect can be cross-cut with the replacement of the strings.

But even with this, it would be a lot easier to get the permission to internationalize the application, as objects suggested.
0
 

Author Comment

by:jerntat
ID: 6378316
Thank You very much for all your help guys. I guess its easier to get the permission.

But I have been using a chinese viewer software that is capable of translating all the windows menubars from english to chinese, Did they do that by manipulating on the window messages or is there some window libraries that can be change to do that?
0
 
LVL 92

Expert Comment

by:objects
ID: 6378338
Don't know much about Windows, so I couldn't tell you.
Does the viewer work with any Windows application?
0
 

Author Comment

by:jerntat
ID: 6378342
Thank You very much for all your help guys. I guess its easier to get the permission.

But I have been using a chinese viewer software that is capable of translating all the windows menubars from english to chinese, Did they do that by manipulating on the window messages or is there some window libraries that can be change to do that?
0
 

Author Comment

by:jerntat
ID: 6378355
Yes it works witn any windows application.
0
 
LVL 92

Expert Comment

by:objects
ID: 6378364
Windows may have a facility for that, I know that strings etc are often kept in resource files similiar to Java.
0
 

Author Comment

by:jerntat
ID: 6382548
Thanks alot guys, I should think through all the options that you guys have provided.Both of you have been a great help! :)
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6383137
Thank you very much for the points. In the future, however, if you find that you want to split the points, there's the possibility to ask Community Support to do this.

Cheers!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Which is the latest version of Apache Tomcatl 3 49
mockito example issue 8 62
session migration servlets 2 23
fibonacci ten numbers 4 22
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now