1

I've come across the following code recently:

Process proc1 = Runtime.getRuntime().exec("C:\\Program Files (x86)\\...");

This is working great. However, an intresting problem. This code actually opens a new instance of the specified process and stores it to the object. It doesn't target that process if its already running and store it to the object. I'm guessing thats the .exec() function call which is doing that. How can I target an already active process and store it to my Process object without opening a new instance?

Note: I tried the obvious answers, Process proc1 = new Process("name"); Eclipse says Process cannot be instantiated. I did lookup the Runtime class in the javadoc. As far as I can tell, there is no obvious method that does what I'm trying to achieve. Seems like a simple qustion, I'm sure its been asked before but I couldn't find the answer anywhere which is why I'm asking here. I may be using the wrong terminology which is why.

EDIT: I'm on Windows 10

My objective is to attatch the process of the game Minecraft to a Process object. Then, I want to move my character forward using robot.keyPress(KeyEvent.VK_W);. I can't just open the process minecraft because all that would do is open the launcher minecraft.exe and then I'd be on a "welcome" screen.

Ashwin Gupta
  • 2,100
  • 7
  • 23
  • 54
  • You can only attach to processes you created. The process has to be a child of the current process. You can gain access to process on Linux using `/proc` but it's platform specific. – Peter Lawrey Feb 11 '16 at 02:51
  • what are you trying to do with the process? – nlloyd Feb 11 '16 at 02:56
  • @PeterLawrey I'm on MS Windows. Sorry forgot to mention that, I added that in to question body. How can I create it in windows? – Ashwin Gupta Feb 11 '16 at 02:56
  • As I understand you do not want to instantiate a process if its already running. But you want to capture the running process into a java Process object right? You can identify your running processes using tasklist.exe of course programatically, then stop that process ( I don't know if that is actually what you want ?) then instantiate a new process. – SomeDude Feb 11 '16 at 02:57
  • @nlloyd just playing around really for learning sake. ATM, I was trying to have my minecraft character move forward by opening the process and using Robot.keypress(). – Ashwin Gupta Feb 11 '16 at 02:57
  • @svasa yes that is what I want. Your method technically works, but in my specific case it actually won't. The process I'm trying to target is the game Minecraft and to run it it needs to go through a launcher. Running the process alone doesn't work. I was hoping I could simply target the already open game. – Ashwin Gupta Feb 11 '16 at 02:59
  • if the answer below was helpful for you would you mind up-voting or accepting it? – nlloyd Feb 15 '16 at 02:51
  • @nlloyd I'm so sorry! this is the second time in the last month I've forgotten to accept an answer. My sincere apologies. +1 and accepted now. I meant to do that a long time ago. – Ashwin Gupta Feb 15 '16 at 20:33

1 Answers1

1

As you can surmise from the Process docs, the process object is created when you create a child process from your code. You can manage the process by using this object, but the other processes out there running are for your OS to manage (or the processes they have affinity to). You'll have to interact with the OS to do anything with them.

An example for finding and killing a process in Windows can be found in this answer.

Community
  • 1
  • 1
nlloyd
  • 1,596
  • 2
  • 14
  • 16
  • I've heard of something called JNI which apprently is for working with the OS and stuff. Might there be some function or class in there that would let me do this? – Ashwin Gupta Feb 11 '16 at 03:01
  • JNI is for interacting with native (think C/C++) code. The OS will not offer this level of access but does typically offer utilities that can be called from the 'command line' using `Runtime.exec()` -- follow the link in my answer to see an example – nlloyd Feb 11 '16 at 03:03
  • Oh okay I didn't know that I was just guessing. Although, if its capable of calling C#, C# may have better Windows process functionality. – Ashwin Gupta Feb 11 '16 at 03:06
  • given the time and resources... anything is possible i suppose. There's no limit to the hackery that one can achieve :) If my boss asked me to kill an unrelated process from my java app right now, I'd go the `Runtime.exec()` route. If this was something you needed scripted - .NET certainly offers options; and in that same vein PowerShell (which runs on .NET but also acts as a shell) [would probably be the easiest to get done quickly.](https://technet.microsoft.com/en-us/library/ee177004.aspx) – nlloyd Feb 11 '16 at 03:13
  • Just found a messy way of doing it. I had it open up the launcher.exe. Then I had the thread sleep for 10 seconds. I manually launched the game from the launcher and opened my world. Then it worked. =/. Not a permanent solution. – Ashwin Gupta Feb 11 '16 at 03:13
  • Alright, well I'll look into this stuff. Thanks for all the advice. I always appreciate learning something new! I'll wait a bit longer to see if anyone else has a different solution before accepting yours. I'm guessing that I'm probably at a dead end here though with using only Java's process functionality like you said. We will see. Happy hacking to you too! – Ashwin Gupta Feb 11 '16 at 03:15