简体   繁体   中英

WebcamCapture working in Netbeans but not in jar file?

Hello Guys I have folowwing problem.

I am using Webcam Capture API to capture Pictures. The Problem is that when i compile everything in Netbeans everything works fine. But if i compile everything to one jar file and then run it again everything works besides that webcam feature. Does anyone of you know where the problem could be because i have no idea anymore.

If i download the example jar file from the page http://www.java2s.com/Code/Jar/w/Downloadwebcamcapture033jar.htm i also cannot start the main jar file.

i already tried to change the JDK versions but it didnt work.

Thank you for your help

The link you point to is just a library jar with no main not an example to be executed.

There are many ways to compile into one jar file, not including all the necessary dependencies could easily create one that will not work.

To create a project that worked for me, create a directory webcamtest.

Save this in the directory as 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>

Make sub directories src\\main\\java and save this as 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());
    }
}

Open the directory as a project in Netbeans and build.

To run single jar file:

java -jar target/webcamtest-1.0-SNAPSHOT-jar-with-dependencies.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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