0

I have written a quick Java wrapper that fires off the NMAP executable and waits for it to finish. I am using Eclipse. I would like to include the NMAP source/executable in my Java jar file so that I have a self-contained package.

Within Eclipse I have added the NMAP folder hierarchy. Within Eclipse I can see Java firing off the NMAP utility correctly, and waiting for the utility to end before exiting.
So far, so good. I then export a JAR file for with eclipse. I can see the NMAP folder hierarchy present. However when I try to run the JAR file it is having trouble finding nmap.exe. Can a foreign executable be called from with a jar file? if not, what are my options? If so, why can't it find it within the jar file when it could find it within Eclipse?

Thanks.

4 Answers 4

1

You will need extract the .exe and its required support files onto the disk before you can access them as regular files (which execution through standard methods requires, I believe). You can look up examples of how to copy a resource from a jar to a file using getResourceAsStream() on one of your class files in the jar.

Once extracted, you can execute is as you are doing now (you might need to ensure execution rights, etc. based on your OS)

Sign up to request clarification or add additional context in comments.

Comments

1

The "execute native program" facility does not understand how to invoke EXE-files inside other files (like ZIP or JAR).

If you want to do this, you must extract the files to a file system location and invoke it there. Due to the diversity of Linux distributions (PowerPC? other library versions etc) you should probably ask the user to install it instead and invoke that instead of bringing your own.

Comments

1

To my knowledge, you cannot execute an executable embedded in a jar file.

One solution would be to embed the executable in the jar file. Then use getClass().getResourceAsStream("/path/to/executable") to retrieve the bytes and output them to a temporary file (File.createTempFile()). On UN*X system, you will have to chmod u+x file before trying to execute. Eventually, you could delete the temp file or create the file once and reuse it everytime and call deleteOnExit().

Of course, this solution implies that you have executable(s) that work on all platforms.

Your solution probably works in eclipse because your "executable" file is not in a jar.

Comments

0

You may also have to be careful of how you are distributing this because Nmap isn't free for use in commercial software.

There is an open source library for Java to call Nmap but it assumes that Nmap is installed on the OS on which you are running your code. That API is Nmap4j and it is on sourceforge.net.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.