English 中文(简体)
Java+jar 文件
原标题:Java + jar file
  • 时间:2012-05-21 15:14:04
  •  标签:
  • java
  • eclipse

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?

谢谢

最佳回答

您需要将. exe 及其所需的支持文件提取到磁盘上, 才能作为常规文件访问它们( 我相信, 使用标准方法执行需要 ) 。 您可以在 jar () 类文件 中, 使用 < code> getReresourcAsStream () 将资源从 jar 复制到文件 。

提取后,您可以像现在一样执行(您可能需要根据您的操作系统确保执行权等)

问题回答

“执行本地程序”设施不理解如何在其他文件(如ZIP或JAR)中援引EXE文件。

如果您想要这样做, 您就必须将文件提取到文件系统位置并在那里引用。 由于 Linux 发行版本( PowerPC? 其他库版本等) 的多样性, 您也许应该请用户安装它, 并引用它, 而不是将它带来自己的 。

据我所知,您无法执行嵌入罐体文件中的可执行文件 。

一种解决办法是将可执行文件嵌入 jar 文件 。 然后使用 < code> getClass (). getResourceAsStream ("/ path/ to/ / excuteable") 来检索字节并将其输出到临时文件 ( File. createTempFile () ) 。 在 UN*X 系统中, 您在尝试执行前必须先使用 < code> chmod u+x 文件 。 最后, 您可以删除临时文件或创建文件一次, 并每一次重新使用它, 并调用 deleteOnExit ()

当然,这一解决方案意味着您拥有在所有平台上工作的可执行文件。

您的解决方案可能在日蚀中有效, 因为您的“ 可执行性” 文件不在罐中 。

你可能还必须小心你是如何分配这个的,因为Nmap不能免费用于商业软件。

Java 有一个开放源库可以调用 Nmap, 但是它假定 Nmap 安装在您运行代码的 OS 上。 该 API is Nmap4j, 并在 sourceforge. net 上 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签