Java 启动独立Jar时,执行提取的*.exe资源失败
我试图在将*.exe文件从项目资源提取到本地/临时路径后执行该文件。当我通过NetBeans运行代码时,它工作得很好。当我为一个独立的Jar构建它时,问题就来了。只是不运行,不提取资源,也不执行资源 为了简洁起见,我省略了实例化Java 启动独立Jar时,执行提取的*.exe资源失败,java,maven,Java,Maven,我试图在将*.exe文件从项目资源提取到本地/临时路径后执行该文件。当我通过NetBeans运行代码时,它工作得很好。当我为一个独立的Jar构建它时,问题就来了。只是不运行,不提取资源,也不执行资源 为了简洁起见,我省略了实例化 public class MyClass { public MyClass() { } public void extractExe() { try { // Get resource as stre
public class MyClass {
public MyClass() {
}
public void extractExe() {
try {
// Get resource as stream
InputStream in = getClass().getResourceAsStream("/resource.exe");
// Set the output path for the stream
OutputStream out = new FileOutputStream("C:/path/to/resource.exe");
// Copy the resource to the path
IOUtils.copy(in, out);
// Close the streams
in.close();
out.close();
// Execute it !
Runtime.getRuntime().exec("cmd /c start C:/path/to/resource.exe");
} catch (FileNotFoundException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
}
}
}
这就是我的pom.xml的样子,它只是加载依赖项,然后构建一个包含库的jar:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jeflopo</groupId>
<artifactId>Myclass</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}-standalone</finalName>
<archive>
<manifest>
<mainClass>com.jeflopo.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
但当我双击.jar时,它不起作用
我不是专家,有人能帮我调试错误吗?我真的不知道我做了什么才能使它再次工作。但它现在起作用了 我已经通过命令行执行了它,我已经运行了命令“mvn package”,但是它仍然无法通过双击启动 我刚去吃饭,回来后我又试了一次。。。现在它启动了 嗯@Sajan Chandran建议显示清单文件,但我认为因为我没有使用maven jar插件,所以我没有清单文件。我只是使用maven assembly plugin将所有依赖项组装到一个可分发的归档文件中 无论如何,谢谢你。
请原谅我的语法,英语不是我的第一语言。发布你的
jar
清单文件。我很想,但我想我的项目中没有这样的文件:$I有pom.xml、nbactions.xml文件、/src和/target文件夹。我已经在项目文件夹中执行了mvn包
。但它并没有为我创建meta inf/manifest.mf。
java -cp myjar.jar com.jeflopo.Main