Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read memory from own process in Java

I'm current attending a course where we have to write an AI to play battleships, and we managed to put out a great working one, but our teacher is a smartass and I'd like to make a cheating AI, that reads the memory and looks where the opponent AI has placed the ships.

The UI is running in a separate thread, where it runs an observer pattern on the logic in the main thread. The positions of the ships is stored in a binary two-dimensional array where true represents a point on a ship (not which, just any ship).

Now the question is: Is it possible to read the memory of the two-dimensional array of enemyBoard somehow, when it is running in the same process and in the same thread?

like image 708
Jan Dragsbaek Avatar asked Mar 05 '26 22:03

Jan Dragsbaek


2 Answers

If it's in the same process and your classes have any kind of link to the driver (and thus indirectly to the other array), you could obtain it using only the reflection API.

like image 164
Konrad Garus Avatar answered Mar 08 '26 12:03

Konrad Garus


One way of doing this would be to call out to a piece of native C/C++ code that uses the JNI interface to copy the array contents from the heap. JNI offers a number of methods for reading/copying and manipulating objects on the heap. The official documentation is a good place to start.

like image 45
mmccomb Avatar answered Mar 08 '26 11:03

mmccomb