0

I have this code which copy the executable file into byte array.

    //winmine.exe
    Path path = Paths.get("winmine.exe");
    byte[] bin = Files.readAllBytes(path);

I want execute dinamically the array without use the file. By example I need something like this:

    Runtime app = Runtime.getRuntime();
    String currentDir = new File(".").getAbsolutePath();
    Process p = app.exec_WRAP_BINARY(bin);   //Not exixts

The purpose of that is prevent the user from accessing the file winmine.exe

Update: I'm sure this must be possible somehow. If we consider that is an execution: First, it read a file and then it load to memory it completely... I don't see because it can't do this separately.

josepmra
  • 617
  • 9
  • 25
  • What is it exactly what you are trying to achieve whit this, what does *The purpose of that is prevent the user from accessing the file winmine.exe* means, access it how, for what I understand you are just trying to execute an '.exe' file, why do you need it to be a Byte array then? – Ordiel May 04 '15 at 19:49
  • I want to distribute PDF's without that the user can open it directly. – josepmra May 04 '15 at 20:41
  • 1
    To open files there's [Desktop.open(File)](http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#open(java.io.File)). Now, where is your PDF? – Raffaele May 04 '15 at 20:50

2 Answers2

1

What you want is not really possible, in general. The system linker will need to link the binary and load it before it executes, and on most (almost every one I know and cetainly Windows/Linux) this means you will need a file on the filesystem.

Of course - you can always write it there, perhaps to a temp directory, execute it as a file, and then delete it later.

Even if you were writing in pure C, you could not easily do this.

(Caveat - On Windows, you /can/ in theory do it with a .com format executable. But severe limitations on that format make it obsolete.)

On Linux, you might also try locating the system loader binary, and execute that, passing the entire binary into it through a pipe. This works because it functions a sort of "binary interpreter". But I would not recommend that approach.

BadZen
  • 4,083
  • 2
  • 25
  • 48
0

Maybe I am understanding what you are asking in a totally wrong way, but if what you are trying to achieve is to have a "private '.exe' file" maybe you should put that as a resource inside the '.jar' that I suppose you want to generate and then extract it to a temp folder "hidden" for the user and call it from there using a simple process call, then when you finish, delete that file without never releasing the lock on it so that the user cannot access it, but all of that is for a "lazy" user, you have to understand that if someone really wants to have access to that exe, THEY WILL, that is the reason Window is still being pirated so easily and all of that is to create a java application that only works on windows, therefore you will need to add another "library/application" to do that for you in *NIX (in case you want to add support for multiple systems)

Ordiel
  • 2,442
  • 3
  • 36
  • 52