• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to issue command in Java program

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Respected Members,
In Java is it possible to issue the command of Ctrl-C (copy) directly from a program, suppose I have many different windows running on my desktop(Win XP) but since in windows at one time only one is active,lets say I have three windows opened one is that of SQL Plus and the other two are some Java applications which I have purchased from some vendor,now one of these Java Application is running I mean its active,is it possible to write a program that issues the command of Ctrl-C after regular intervals, as a consequence any text written in these Java windows would be Copied,please guide me how can this be possible,I would be thankful to you from the depth of my heart if you can guide me to the solution

Thanking You,
Bilal Ali
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, probably not. There may be an OS specific route to do this - if there is, its not one I know of.

But more importantly - why? What are you tring to achieve by randomly copying stuff to the clipboard?
 
Bilal Ali
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Respected Member,
Thanks for your help and reply , I will try to be more specific in this post and state the excat problem,actually the Ctrl-C command would not be executed randomly after regular intervals of time,infact it would be executed when the user presses some hot key lets say Ctrl-F1,on pressing Ctrl-F1 immidiately my program will going to trap this event and issue a command of Ctrl-C.

Now to the more important problem that why do I need to write such a program,actually dear friend our organization purchased a software(lets say it XYZ Product) from a vendor, its basically a Java swing application,now we want some validations to be applied on certain values which are enetered in the text feild,the vendor now is not ready to do it for us,he has also refused to give us the source code or any API by which we could have interacted with this software,so we thaught that it would be a good idea to develop a Java program which would be running in memory like a windows process/service when the user enters Ctrl-F1 than our program will trap this event,issue Ctrl-C,after this text would automatically be shifted in clipboard, than gets the selected text from the clipboard and than assigned it to the string,once we have this value in our hand than we can apply necessay validations on it.

So you see the only issue here is to get Ctrl-C some how executed from program, rest is no problem.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do it in using C++ etc i.e. treat the swing as just another windows window , it can hook the keyboard and fake events whatever you could JNI back into java if you wanted to do your validation in java but its going to be easier to do this native to Windows.

I'm NOT recommending this method by the way just saying it is possible. Presumably he hasn't given you the source and your not running in his application so failing that you'ed be reverse engineering the java from the byte code ... hmmm ... what about the accessibility classes you might be able to do it that way as well, google them ;-) .. again its going to be dirty and nasty ;-)
 
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bilal Ali:
, its basically a Java swing application,now we want some validations to be applied ................



You could do the following.

1. Reverse engineer the class files using the decompilers available
2. Extend the Form classes which are the screens in the software delivered to you
3. Plug in your validations in your form classes and if they pass delegate the call to the parent class using super.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. Reverse engineer the class files using the decompilers available


This is very bad advice unless the licence of this applciation allows it.


Now to the more important problem that why do I need to write such a program,actually dear friend our organization purchased a software(lets say it XYZ Product) from a vendor, its basically a Java swing application,now we want some validations to be applied on certain values which are enetered in the text feild,the vendor now is not ready to do it for us,he has also refused to give us the source code or any API by which we could have interacted with this software,so we thaught that it would be a good idea to develop a Java program which would be running in memory like a windows process/service when the user enters Ctrl-F1 than our program will trap this event,issue Ctrl-C,after this text would automatically be shifted in clipboard, than gets the selected text from the clipboard and than assigned it to the string,once we have this value in our hand than we can apply necessay validations on it.



To my mind your proposed solution is not a very good one. If you really need this validation copying to a clipboard and overwriting is a flaky route to choose. Where does the data you enter into this application end up? In a database?
 
Bilal Ali
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am once again thankful to the all the members of Java Ranch for replying to my questions and and taking out there precious time to guide me.

As Mr Chris Hurst said in his reply:

You could do it in using C++ etc i.e. treat the swing as just another windows window , it can hook the keyboard and fake events whatever you could JNI back into java.....



sir, is the same possible with VB or C#,actually I have never worked on C++ but have experience in VB and C#,are you trying to say that write some kind of function in C++ which would be copying the selectd text from the text feild to the clipboard,than call this function from Java using JNI and since once the function has shifted the selected text to the clipboard than accessing it from there is not a difficult task.....this is what I have interpreted from your reply so is that what you are saying sir....???

As another friend Mr Devesh H Rao said:



actually sir you are very right, we can get the source code by reverse engineering the code but its a very very big risk for us beacuse this application is really a huge application as a matter of fact very very very..... huge application its running live on 200 different locations and hundreds of users are using it so if any thing goes wrong in the source code than we developers would be completely responsible for it which could also esult in severe action against us from the Management.Lastly our Project Manager has refused to use this solution so we can'nt go any furtther with it.

As Mr Paul Sturrock said:

To my mind your proposed solution is not a very good one



but sir we dont have any other way out...thats the only solution we could have found....if you can suggest some better solution than we would really be thankful to you.... and yes the data which we enter ends up in a database but as a requirement before letting the data to the database we have to apply some validations on it.

In the end I want to put forward another question that:

Why does'nt Java allow us to access any kind of active window running on the screen, if could access any active window with VB,C++ than whats the problem with Java, and are there any kind of APIs by which we can access or any third party open source avaiable by which we can interact with active windows, because if there is any such stuff than we could easily use it to aceess the Java Swing application and any selected text in it,because this Java Swing application is actually the active window,any idea or some better solutions than please reply I would be waiting for your suggestions.....Thanks in advance
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


but sir we dont have any other way out...thats the only solution we could have found....if you can suggest some better solution than we would really be thankful to you.... and yes the data which we enter ends up in a database but as a requirement before letting the data to the database we have to apply some validations on it.


Ah, well to my mind here is a better integration point for extra adhoc business logic. You have an applciation that you cannot dependably change, the only solutions are a bit clucky. But presumably you own the database, so you can add workarounds there? What I'd do is see if I can influence how this application connects to the database (assign a distinct user for it for example) and possibly have some holding tables the applciation data goes into then use something like triggers to convert that dat into what your applciation's business rules require. Is this something you can consider?
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Sturrock:

This is very bad advice unless the licence of this applciation allows it.





Point 1.

Our organization purchased a software(lets say it XYZ Product) from a vendor, ..........

Point 2.
the vendor now is not ready to do it for us,he has also refused to give us the source code or any API by which we could have interacted with this software.................



If I purchase something, i expect due support in maintanence and enhancements. The vendor is behaving like a guy with a gun at a hold up, any ways this is something for the legal department to handle.

And if you go through my post, i mentioned extending the classes not fiddling around with the reverse engineered source. Reverse engineering would not have been suggested by me if the vendor had provided proper api's and documentation which they are supposed to do along with a product which they obviously haven't.

To be more clear in what i suggested, It would be not exactly right to reverse engineer java.lang.String and add methods to it but i guess i can always extend java.lang.String and do whatever i want with the extended class.

Please correct me if i got this wrong.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Devesh H Rao:
If I purchase something, i expect due support in maintanence and enhancements. The vendor is behaving like a guy with a gun at a hold up, any ways this is something for the legal department to handle.

And if you go through my post, i mentioned extending the classes not fiddling around with the reverse engineered source. Reverse engineering would not have been suggested by me if the vendor had provided proper api's and documentation which they are supposed to do along with a product which they obviously haven't.



Well, you suggested using a decompiler. Just about any software license (and remember, most software is licensed, not sold) explicitly forbids this. It makes no difference how bad the service is that the software vendor provides. One may be able to take the vendor to court over this (if they don't provide something that the contract stipulates), but the license is still binding.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Devesh H Rao:

java.lang.String is final, so extending it isn't possible.

But to the original question.
Afaik you can get the content of the clipboard.

Here is a sample code, which is based on code I found myself in a forum, hence the comment in line 1.

processContents is the method you would replace (at least).
In it's current state it simply puts code-tags (which were obfuscated, to get around the layout-problem, because this forum is sensitiv for exactly these code-tags) around the clipboard-text, and works with linux/x11, but was developed for a ms-environment (see bottom).

It does not fullfill your requierement to work only for a single Textbox, but will decorate every text in the clipboard.
Perhaps you can inspect the content and analyze, whether this is coming from the textbox or not.

Or you could define a Windows-Hotkey, which will fire up this listener, and tell the users, how to use it.

I guess you would get better answers in the Swing/AWT/...-section.

SQL-Plus indicates you're using oracle, and as suggested above, a different approach could be, to use an insert- or update-trigger, and the best adress for such a question would be the JDBC-section or the oracle-forum.
---
Ha!
Guess where I got the code from?
https://coderanch.com/t/377833/java/java/listen-clipboard
[ July 15, 2007: Message edited by: Stefan Wagner ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

... we want some validations to be applied on certain values which are entered in the text field,the vendor now is not ready to do it for us,he has also refused to give us the source code or any API by which we could have interacted with this software,so we thought that it would be a good idea to develop a Java program which would be running in memory like a windows process/service ...



In another recent thread I mentioned some commercial tools that can do this kind of thing: Jacada, OpenSpan, E-Glue are some that I've looked at. They are definitely not Java because they dig way deep into Windows APIs.
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:


Well, you suggested using a decompiler. Just about any software license (and remember, most software is licensed, not sold) explicitly forbids this. It makes no difference how bad the service is that the software vendor provides. One may be able to take the vendor to court over this (if they don't provide something that the contract stipulates), but the license is still binding.



Yes you are right, I did a bit of homework and what you say is the the way it is in most of the cases. So in short I am screwed if I get into a deal with a vendor who then backtracks

More power to open source....

Originally posted by Stefan Wagner


@Devesh H Rao:

java.lang.String is final, so extending it isn't possible.




It was used for illustrating a point, the point was important not the example which I thought was pretty implicit. Anyways my bad it does not seem to be the case. next time onwards I will be careful with that as well.

its basically a Java swing application,now we want some validations to be applied on certain values which are enetered in the text feild



And also the link provided contains a method to work on any clipboard data, I guess it will not work in this case as the data is related to a specific textfield or textfield's whatever the case may be.



This seriously is a nice question.. Bilal Ali thanks for the workout...

Bilal, a question for you, is the swing UI resizable. The reason I am asking this is if the screen is not resizable I can benchmark on a area on the screen where the keystrokes have to be captured.. and that is kind of giving me an idea..?
[ July 16, 2007: Message edited by: Devesh H Rao ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


@Devesh H Rao:
java.lang.String is final, so extending it isn't possible.


It was used for illustrating a point, the point was important not the example which I thought was pretty implicit. Anyways my bad it does not seem to be the case. next time onwards I will be careful with that as well.



I knew, and used that small error to illustrate the possibility, the Textfield, or it's containing component could be final too.
I should have made that explicit.

However - the license wouldn't allow replacement - do we like closed source?


its basically a Java swing application,now we want some validations to be applied on certain values which are enetered in the text field



And also the link provided contains a method to work on any clipboard data, I guess it will not work in this case as the data is related to a specific textfield or textfield's whatever the case may be.


That's why I mentioned to investigate, whether the content of the text could be analyzed, whether it is coming from this field.

I like your Idea of measuring the fieldsize.

Since the hack shall work on an inhouse-software only, a solution which needs some care from the user, like hitting a button before the cut and paste is done, might be appropriate.

[ July 17, 2007: Message edited by: Stefan Wagner ]

[ July 17, 2007: Message edited by: Stefan Wagner ]
(Damned 'quote(quote)'-quoting!)
[ July 17, 2007: Message edited by: Stefan Wagner ]
 
Bilal Ali
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Respected Members,
I am once again thankful to all of you from the depth of my heart for your overwhelming response,I am really very tahkful tat all of you gave me your precious time, I know very well that many of you might be professional developers but still taking out your time to guide me,however inspite of googling and consulting many different sources I have not moved more than few inches forward and as a matter of fact I am badly stucked with no way out,I will ty to answer some of the answers which the readers raised:

As Mr. Paul Sturrock said

use something like triggers to convert that dat into what your applciation's business rules require....



For this we will have to contact the DBA and I am sure that he will never going to allow us,because if any thing goes up or down than the entire responsiblity will be on him so the DBA is'nt ready to take some risk.

As Mr. Devesh H Rao said:

Bilal, a question for you, is the swing UI resizable



yes the swing is resizable,and it can be any where on the screen.

now once again I have two more questions, infact one is the solution which I have thinked of and another is a question.

SOLUTION WHICH I HAVE THOUGHT

as a result of all my searching I have come to the conclusion that in Java we cannot access a active window(If I am wrong than please correct me), however a fellow developer showed me his work in which he accessed the active window and the selected text in it using VB, I am thinking to call this VB function from my Java program using JNI,now what actually my Java program will be doing is that I will trap the Ctrl-F1 hot key from the user and call this function of VB using JNI,this function of VB will access the active window,get the selected text and insert in the clipboard,nwo as I previously said getting value from clipboard is not a problem from us.....

do you people agree with this solution, do you think that its the best solution, off course at the start I was aiming for a pure Java solution but again I think that there is no way out, I would again be waiting for your suggestions and precious comments.

now lets come to the question which I previously also asked

Why does'nt Java allow us to access any kind of active window running on the screen, if could access any active window with VB,C++ than whats the problem with Java, and are there any kind of APIs by which we can access or any third party open source avaiable by which we can interact with active windows, because if there is any such stuff than we could easily use it to aceess the Java Swing application and any selected text in it,because this Java Swing application is actually the active window,any idea or some better solutions than please reply I would be waiting for your suggestions.....Thanks in advance

 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every JVM has it's own memory, and can't access the memory of another one, which is a good idea from a security viewpoint.

Imagine a suspicious entertainment software from the web, accessing your online-banking-software.

But perhaps it's technically possible, to circumvent the separation of memory, by starting both programs - your validator and the 3rd-party-program X in the same JVM.

Assume, X is written in a way, that X extends JFrame, and is started by calling X.main, and main is implemented:

Now it would be simple, to write your Validator, crating X itself:

and search for your Component.
If you can identify it, you may add a Listener to it.

That approach depends on the way, how X is implemented.
Perhaps it's more complicated or impossible at all.
Maybe main is doing much more things to start X, which is hard to find out, maybe the constructor of X is private, to avoid such hacks.

And whether this approach is legal: I don't know.
 
Bilal Ali
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
Good news for many of you,at least its for me..... I think I have some what succded in getting a pure Java solution without using JNI or Win 32 API, I found that in Java we have a class named java.awt.Robot , with the help of this Robot class we can generate any command or virtually any key on your key board or the combination of several key, now since COPY function is Ctrl-C, I used the following lines of code to generate copy command, Thank God that the text which I had selected was succesfully copied,inserted to the System Clipboard as a result and as I have previously said that getting some text from the key board was non big issue for us, so now some how the value from this value from the Vendor's software is finally in our hands, before going any further let me first tell the code:



The code virtually presses the Ctrl key than the "C" key and tahn release both of these... COPY command has been isued



but there is still one more problem in hwich I need your help....

I want to perform this entire task when the user presses Ctrl-F3(both Ctrl and F3 keys together),please also note that our program will be running in the memory like a windows service/process at the back end,

so my question is that

how can I trap the Ctrl-F3 key press, I mean to say that on what basis will check the Ctrl-F3 key press.Please reply soon like you all have guided me in the past.
[ July 20, 2007: Message edited by: Bilal Ali ]
 
Bilal Ali
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please some one reply that how do I trap the key stroke of Ctrl-F3 in Java when my program is running in the back end inside the Windows process, it seems that not many members are intersted in replying,please guide me as all of you have guided me in the past,please reply soon.... I would be waiting for your precious replies.

Thanking You,
Bilal Ali.
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bilal Ali:
Please some one reply that how do I trap the key stroke of Ctrl-F3 in Java when my program is running in the back end inside the Windows process, it seems that not many members are intersted in replying,please guide me as all of you have guided me in the past,please reply soon.... I would be waiting for your precious replies.

Thanking You,
Bilal Ali.



Write a macro in C and init it using the native mechanism using your java code... the macro will basically listen for keystrokes at the memory location of the keyboard... I guess that takes care of the "Java when my program is running in the back end" thing

I am sorry this is the first thing that came to my mind... there must be a better way surely.
 
Chris Hurst
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are a couple of Java libs out there that'll do it if you google for them eg JIntellitype.

I have written what you want before, but my code involved JNI to a C++ DLL (C# is fine) and registering either a keyboard hook or with windows hot keys api (see msdn for examples). In my case I had a main java window I wanted to pop up so I located the real handle using the AWT dll and then activated it from C++ in response to the key combo (for visual stuff you have to be careful with threads). I wrapped all this with a java layer so all you did was register a key combo in java and this became a faked Swing key event ehen required ie Swing got key presses even when not in the foreground.
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic