Author Topic: Can Java program interact with other programs  (Read 1645 times)

0 Members and 1 Guest are viewing this topic.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Can Java program interact with other programs
« on: 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)

Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Can Java program interact with other programs
« Reply #1 on: 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!
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline deadlysin

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
Re: Can Java program interact with other programs
« Reply #2 on: 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.


Offline Matriplex

  • Knight
  • **
  • Posts: 323
  • Cookies: 66
  • Java
    • View Profile
Re: Can Java program interact with other programs
« Reply #3 on: 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.
\x64\x6F\x75\x65\x76\x65\x6E\x00

Offline BadVibes

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: Can Java program interact with other programs
« Reply #4 on: 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
Code: [Select]
Robot robot = new Robot();
Then you'd move to where ever you have your buttons that need pressing
Code: [Select]
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
Code: [Select]
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
Code: [Select]
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.
« Last Edit: July 15, 2014, 01:36:05 am by BadVibes »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Can Java program interact with other programs
« Reply #5 on: 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.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Can Java program interact with other programs
« Reply #6 on: 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

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Can Java program interact with other programs
« Reply #7 on: 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

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.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Can Java program interact with other programs
« Reply #8 on: 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.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Can Java program interact with other programs
« Reply #9 on: July 15, 2014, 10:38:32 am »
Ok, thanks for answers.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Can Java program interact with other programs
« Reply #10 on: 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/
« Last Edit: July 15, 2014, 11:36:21 am by Kulverstukas »

Offline RedBullAddicted

  • VIP
  • Sir
  • *
  • Posts: 519
  • Cookies: 189
    • View Profile
Re: Can Java program interact with other programs
« Reply #11 on: 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 :)
Deep into that darkness peering, long I stood there, wondering, fearing, doubting, dreaming dreams no mortal ever dared to dream before. - Edgar Allan Poe

Offline nulldigit

  • Serf
  • *
  • Posts: 28
  • Cookies: -2
  • syntax sugar om nom
    • View Profile
Re: Can Java program interact with other programs
« Reply #12 on: 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.
N/A

Offline BadVibes

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: Can Java program interact with other programs
« Reply #13 on: 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.

Offline DreX

  • Serf
  • *
  • Posts: 42
  • Cookies: -5
    • View Profile
Re: Can Java program interact with other programs
« Reply #14 on: 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.