简体   繁体   English

Java + jar文件

[英]Java + jar file

I have written a quick Java wrapper that fires off the NMAP executable and waits for it to finish. 我编写了一个快速的Java包装程序,该包装程序会启动NMAP可执行文件并等待其完成。 I am using Eclipse. 我正在使用Eclipse。 I would like to include the NMAP source/executable in my Java jar file so that I have a self-contained package. 我想在我的Java jar文件中包含NMAP源代码/可执行文件,以便拥有一个独立的软件包。

Within Eclipse I have added the NMAP folder hierarchy. 在Eclipse中,我添加了NMAP文件夹层次结构。 Within Eclipse I can see Java firing off the NMAP utility correctly, and waiting for the utility to end before exiting. 在Eclipse中,我可以看到Java正确触发了NMAP实用程序,并在退出之前等待该实用程序结束。
So far, so good. 到现在为止还挺好。 I then export a JAR file for with eclipse. 然后,我使用eclipse导出JAR文件。 I can see the NMAP folder hierarchy present. 我可以看到存在NMAP文件夹层次结构。 However when I try to run the JAR file it is having trouble finding nmap.exe. 但是,当我尝试运行JAR文件时,无法找到nmap.exe。 Can a foreign executable be called from with a jar file? 可以使用jar文件从外部调用可执行文件吗? 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? 如果是这样,为什么在Eclipse中可以在jar文件中找不到它呢?

Thanks. 谢谢。

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). 您需要将.exe及其必需的支持文件提取到磁盘上,然后才能将它们作为常规文件进行访问(我相信通过标准方法执行该文件是必需的)。 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. 您可以查找有关如何在jar中的一个类文件上使用getResourceAsStream()将资源从jar复制到文件的示例。

Once extracted, you can execute is as you are doing now (you might need to ensure execution rights, etc. based on your OS) 提取后,您可以像现在一样执行(您可能需要根据操作系统确保执行权限等)

The "execute native program" facility does not understand how to invoke EXE-files inside other files (like ZIP or JAR). “执行本机程序”功能不了解如何在其他文件(如ZIP或JAR)中调用EXE文件。

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. 由于Linux发行版(PowerPC?其他库版本等)的多样性,您可能应该要求用户安装它,然后调用它而不是自带。

To my knowledge, you cannot execute an executable embedded in a jar file. 据我所知,您无法执行jar文件中嵌入的可执行文件。

One solution would be to embed the executable in the jar file. 一种解决方案是将可执行文件嵌入jar文件中。 Then use getClass().getResourceAsStream("/path/to/executable") to retrieve the bytes and output them to a temporary file ( File.createTempFile() ). 然后使用getClass().getResourceAsStream("/path/to/executable")检索字节并将其输出到临时文件( File.createTempFile() )。 On UN*X system, you will have to chmod u+x file before trying to execute. 在UN * X系统上,您必须先chmod u+x file然后再尝试执行。 Eventually, you could delete the temp file or create the file once and reuse it everytime and call deleteOnExit() . 最终,您可以删除临时文件或创建一次该文件,然后每次重用它,然后调用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. 您的解决方案可能在Eclipse中工作,因为您的“可执行”文件不在jar中。

You may also have to be careful of how you are distributing this because Nmap isn't free for use in commercial software. 您可能还必须注意如何分发它,因为Nmap并非免费用于商业软件。

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. 有一个用于Java的开源库可调用Nmap,但它假定在运行代码的OS上安装了Nmap。 That API is Nmap4j and it is on sourceforge.net. 该API是Nmap4j,位于sourceforge.net上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM