Solved

Intercepting Java application output

Posted on 2001-08-09
34
296 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
Comment Utility
Certainly, you just need an app that reads from stdout.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Just need to pipe output to your other app:

java abc | myapp
0
 

Author Comment

by:jerntat
Comment Utility
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
Comment Utility
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
Comment Utility
In fact, as far as I know, there is a project using BCEL to implement aspects... :-)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
Use ResourceBundle.
0
 
LVL 3

Expert Comment

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

Author Comment

by:jerntat
Comment Utility
but i dont have the source file...
0
 
LVL 92

Expert Comment

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

Expert Comment

by:dnoelpp
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ah, I understand, you have an other application and you need to replace its strings...
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 92

Expert Comment

by:objects
Comment Utility
> I try not to decompile...

Good to hear.

> technically still the same i suppose....

Certainly is :)


0
 
LVL 3

Expert Comment

by:dnoelpp
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> for example going really low level...

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

Expert Comment

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

Expert Comment

by:objects
Comment Utility
> I am sorry, I just discovered that I duplicated a post from objects

No worries, it happens.
0
 
LVL 3

Expert Comment

by:dnoelpp
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes it works witn any windows application.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

771 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

12 Experts available now in Live!

Get 1:1 Help Now