Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

Intercepting Java application output

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
jerntat
Asked:
jerntat
  • 12
  • 11
  • 11
1 Solution
 
objectsCommented:
Certainly, you just need an app that reads from stdout.
0
 
objectsCommented:
Just need to pipe output to your other app:

java abc | myapp
0
 
jerntatAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dnoelppCommented:
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
 
dnoelppCommented:
In fact, as far as I know, there is a project using BCEL to implement aspects... :-)
0
 
objectsCommented:
> 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
 
jerntatAuthor Commented:
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
 
objectsCommented:
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
 
dnoelppCommented:
Use ResourceBundle.
0
 
dnoelppCommented:
Read the javadoc about java.util.ResourceBundle. This class is exactly for this type of thing you need.
0
 
jerntatAuthor Commented:
but i dont have the source file...
0
 
objectsCommented:
Legal issues aside, you could simply decompile it.
0
 
dnoelppCommented:
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
 
jerntatAuthor Commented:
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
 
jerntatAuthor Commented:
i mean i dont have the source code to the application class file whoose output i wanted to alter.
0
 
dnoelppCommented:
Why? Using ResourceBundle is the standard way of implementing multi-language applications. I wouldn't use BCEL or AspectJ to implement multi-language.
0
 
dnoelppCommented:
Ah, I understand, you have an other application and you need to replace its strings...
0
 
objectsCommented:
> I try not to decompile...

Good to hear.

> technically still the same i suppose....

Certainly is :)


0
 
dnoelppCommented:
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
 
jerntatAuthor Commented:
i mean i dont have the source code to the application class file whoose output i wanted to alter.
0
 
dnoelppCommented:
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
 
jerntatAuthor Commented:
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
 
jerntatAuthor Commented:
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
 
objectsCommented:
> for example going really low level...

Re-engineering the entire app sounds easier :-)
0
 
objectsCommented:
Have you considered asking the original author permission to i18n the code?
0
 
objectsCommented:
> I am sorry, I just discovered that I duplicated a post from objects

No worries, it happens.
0
 
dnoelppCommented:
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
 
jerntatAuthor Commented:
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
 
objectsCommented:
Don't know much about Windows, so I couldn't tell you.
Does the viewer work with any Windows application?
0
 
jerntatAuthor Commented:
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
 
jerntatAuthor Commented:
Yes it works witn any windows application.
0
 
objectsCommented:
Windows may have a facility for that, I know that strings etc are often kept in resource files similiar to Java.
0
 
jerntatAuthor Commented:
Thanks alot guys, I should think through all the options that you guys have provided.Both of you have been a great help! :)
0
 
dnoelppCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 11
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now