0

大家好,我有以下问题。

我正在使用 Webcam Capture API 来捕获图片。问题是,当我在 Netbeans 中编译所有内容时,一切正常。但是,如果我将所有内容编译到一个 jar 文件中,然后再次运行它,那么除了网络摄像头功能之外,一切都可以正常工作。你们有谁知道问题可能出在哪里,因为我不知道了。

如果我从页面http://www.java2s.com/Code/Jar/w/Downloadwebcamcapture033jar.htm下载示例 jar 文件, 我也无法启动主 jar 文件。

我已经尝试更改 JDK 版本,但没有奏效。

谢谢您的帮助

4

1 回答 1

0

您指向的链接只是一个没有 main 的库 jar,不是要执行的示例。

有很多方法可以编译成一个 jar 文件,不包括所有必要的依赖项很容易创建一个不起作用的。

要创建一个适合我的项目,请创建一个目录 webcamtest。

将其保存在目录中pom.xml

<?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.github.wshackle</groupId>
    <artifactId>webcamtest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.github.sarxos</groupId>
            <artifactId>webcam-capture</artifactId>
            <version>0.3.10</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <main.class>Main</main.class>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>${main.class}</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

创建子目录src\main\java并将其另存为Main.java

import com.github.sarxos.webcam.Webcam;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {

    public static void main(String[] args) throws IOException {
        Webcam webcam = Webcam.getDefault();
        webcam.open();
        File f =  new File("webcam_snap.png");
        ImageIO.write(webcam.getImage(), "PNG",f);
        System.out.println("Saved image "+f.getAbsolutePath());
    }
}

在 Netbeans 中将目录作为项目打开并构建。

运行单个 jar 文件:

java -jar target/webcamtest-1.0-SNAPSHOT-jar-with-dependencies.jar
于 2015-09-18T22:30:06.770 回答