EvilZone

Community => General discussion => : DreX July 14, 2014, 10:25:22 PM

: Can Java program interact with other programs
: DreX July 14, 2014, 10:25:22 PM
I am currently learning Java and have a question:
Situation:
     I have instaled a program (500+). I need my Java program to be able to read value from this program and to be able to interact with it  (click buttons for me).
Can this be done in Java (if so, in which chapter (If Statement, Loops...) would I found this)
or
in what program language should I look next (friends suggested C Sharp)
: Re: Can Java program interact with other programs
: Matriplex July 15, 2014, 12:57:01 AM
Well generally it's very difficult to get Java to do such things simply because it runs inside its own virtual machine (the JVM). However, and I don't recommend this, you can use the Robot class to click on exact pixel positions and move the mouse among other things. But this probably isn't suited for your needs.

I would look into C# like your friend suggested. If you know Java, it's very similar, and there are tons of benefits to using the language (linq statements, lambdas, operator overloading, class extension methods and so on). However keep in mind that C# is made to run on the Windows OS.

Good luck!
: Re: Can Java program interact with other programs
: deadlysin July 15, 2014, 01:08:20 AM
You can use JNI (Java Native Interface) to interact with OS DLLs, and do the same thing that C can do.

: Re: Can Java program interact with other programs
: Matriplex July 15, 2014, 01:19:34 AM
You can use JNI (Java Native Interface) to interact with OS DLLs, and do the same thing that C can do.

There is JNI, but be warned it's a total pain in the ass.
: Re: Can Java program interact with other programs
: BadVibes July 15, 2014, 01:24:08 AM
You can use Java to do this. You'd have to (well not have to, but probably easiest) use Javas robot class (Java.awt.Robot).

You'd have to create a bot
:
Robot robot = new Robot();
Then you'd move to where ever you have your buttons that need pressing
:
robot.mouseMove(500,500);
The 500, 500 args are x and y. X comes from the left and Y from the top

now to press something, you would
:
robot.mousePress(InputEvent.BUTTON1_MASK);
BUTTON1_MASK is for a left click
BUTTON2_MASK is for a middle click
BUTTON3_MASK is for a right click


Also, you may want to do something such as
:
robot.delay(2000);
before you make it click, or you won't see the mouse move as its instant.

The delay is in milliseconds, so the delay will make you wait for 2 seconds.
That should do everything you ask for.
: Re: Can Java program interact with other programs
: Kulverstukas July 15, 2014, 06:16:51 AM
I am not sure if a reliable way exists with Java, it was made for difderent things. However since you are open to language suggestions, I recommend Python and Pywinauto lib and Swapy GUI.
I used those in the past and they worked great, but ofcourse only for windows and at the beginning might be hard to get it right.
: Re: Can Java program interact with other programs
: Deque July 15, 2014, 08:35:04 AM
Well generally it's very difficult to get Java to do such things simply because it runs inside its own virtual machine (the JVM).[...]
I would look into C# like your friend suggested.

You should realize that C# runs in a VM as well, the CLR: https://en.wikipedia.org/wiki/Common_Language_Runtime
Your reasoning isn't valid, because it has nothing to do with Java running in a JVM. It has rather to do with Java being made for portability and not for accessing OS specific functions. You can do it, but it would mean to destroy the portability, which raises the question, why someone would use Java in this case.

@DreX: What kind of other program? What OS? Sometimes there are simpler ways around it. Really depends on the situation.
But in general for Windows, you should have a read here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645530%28v=vs.85%29.aspx
: Re: Can Java program interact with other programs
: DreX July 15, 2014, 09:55:27 AM
Thank you all for your answers.

@DreX: What kind of other program? What OS? Sometimes there are simpler ways around it. Really depends on the situation.
But in general for Windows, you should have a read here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645530%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/ms645530%28v=vs.85%29.aspx)

OS - Windows 7 (64)
The program I want my Java to interact with is +500. +500 is a simple stock exchange program. I want my Java to be able to check the value of a stock from +500 and after comparing it to previous value decide if (buy/keep) or (sell) stock.
I have the comparing and the loops pretty much down. Its the "check the value from other program" that will prove difficult.
: Re: Can Java program interact with other programs
: Deque July 15, 2014, 10:07:56 AM
Thank you all for your answers.

OS - Windows 7 (64)
The program I want my Java to interact with is +500. +500 is a simple stock exchange program. I want my Java to be able to check the value of a stock from +500 and after comparing it to previous value decide if (buy/keep) or (sell) stock.
I have the comparing and the loops pretty much down. Its the "check the value from other program" that will prove difficult.

I see. Unfortunately it doesn't seem to provide an API, which makes it difficult to interact with it.
Maybe check for alternative programs with an API.
Or you really have to go through some trouble to make it work.
: Re: Can Java program interact with other programs
: DreX July 15, 2014, 10:38:32 AM
Ok, thanks for answers.
: Re: Can Java program interact with other programs
: Kulverstukas July 15, 2014, 11:35:56 AM
Consider my suggestion for such things, or there is another thing just for this, however I haven't tried it myself. It's called Scar and it's a macro scripting engine for windows, based on the Pascal language: http://www.kaitnieks.com/scar/
: Re: Can Java program interact with other programs
: RedBullAddicted July 15, 2014, 02:54:36 PM
Not a answer to your question but I would try to figure out how the +500 Application gets the data. I am pretty sure it somehow queries one or more online services and just displays the information. Give it a go with wireshark and maybe you are able to query the same online ressources with your java program. That would be my first attempt before I would try to figure out how to interact with a program that does not provide an api. I am not an expert and I never dealed with stocks in any form but all the displayed informations in that program should be publicly available :)
: Re: Can Java program interact with other programs
: nulldigit July 15, 2014, 04:05:00 PM
I agree with rba here. There is much eaiser solutions online. If you can query and POST the data, it would be cake.
: Re: Can Java program interact with other programs
: BadVibes July 15, 2014, 04:43:35 PM
Ok, thanks for answers.
If you search on info about Java Robots you should be able to find info on this. Worsat case scenario, you'd have to just make it hover over text, manually copy it, and paste it into a program that checks the value. If you describe what you want this 'value checking' program to do, I could probably help you.
: Re: Can Java program interact with other programs
: DreX July 15, 2014, 11:39:29 PM
If you search on info about Java Robots you should be able to find info on this. Worsat case scenario, you'd have to just make it hover over text, manually copy it, and paste it into a program that checks the value. If you describe what you want this 'value checking' program to do, I could probably help you.

Nah, no need. So far I want to do it myself. To learn through practice.
But thanks anyway.