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
Related
I've created a JavaFX GUI and it worked fine until I exported it as a JAR file and converted it to an Executable file.
Running it using VS Code Run and Debug works fine but, whenever I open the executable, which I exported from
and converted using Launch4j, nothing happens.
But when I try to run it on the Terminal using
Java -jar <filename>.jar
It shows the error
Error: Could not find or load main class ?jar
Caused by: java.lang.ClassNotFoundException: ?jar
below is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Launch App",
"request": "launch",
"mainClass": "infixToPostfix.App",
"vmArgs": "--module-path D:/Programming/Projects/School/javafx-sdk-19/lib --add-modules javafx.controls,javafx.fxml"
}
]
}
and bellow is my folder structure
I've seen a few post that is related to the one I currently have but nothing helped. I'm new to JavaFX and i've been trying different solutions for hours. Maybe I just missed something important. Thank you in advance.
I've checked similar post but most of them are using IntelliJ or Eclipse. I've also seen some which uses VS Code and when I tried them, still nothing worked.
Config file
The error you got is a very similar one I got months ago when I first started to develop a JavaFX application. I needed to tell Maven what class my Main class was so that Maven could compile a .jar file for me
I understand your issue. With Maven we need to assign the Main class in the config file, like this: ${project.mainClass} I don't know where you need to do it. Maybe in settings.json or launch.json. You need to check the Java VSCode plugin which provides you with the export function. Check the documentation and it will tell you where you need to put the Main class in the configuration file.
Example pom.xml file (Maven config file)
This is a Maven example and you need to do something similar. It's important that you also compile and tell the Java plugin to compile a single fat JAR with dependencies, otherwise, it won't run.
<?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>
<name>My example project</name>
<groupId>nl.hva</groupId>
<artifactId>my_example_artifact</artifactId>
<version>1.0.0.</version>
<packaging>jar</packaging>
<properties>
<javafx.version>19</javafx.version>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.mainClass>my_example_package_name.Main</project.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>23.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath/>
<addDefaultImplementationEntries/>
<addDefaultSpecificationEntries/>
<addExtensions/>
<classpathLayoutType/>
<classpathPrefix/>
<customClasspathLayout/>
<mainClass>${project.mainClass}</mainClass>
<packageName/>
<useUniqueVersions/>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
<key>value</key>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
VS Code docs
VS Code docs also mention using Maven! So it's easy for me to help you because I have workable JavaFX 19 applications (in another IDE, but Maven is the same everywhere). See vscode docs: https://code.visualstudio.com/docs/java/java-project
Currently the speed to run a test function is about 5× slower when done with the jar file vs. doing it in Eclipse. How should I install the jar file so the speed is similar?
I am using maven. I am using outside dependencies. I just need to know what is the best code for the build (in the pom file) to make it run as fast as possible, with no concern for copy rights. The only thing I need is for the program to work on a machine without maven installed.
Also, based on the last time I asked this, I will add more info that might be useful. Java is up to date. All is stored on the C drive. There is no outputs that is slowing this down, and it is all text based. There is a lot of reading and writing on files going on that take time to do, but it took 16.6 seconds using Eclipse and 89.6 using the jar file.
Here is the pom file, including the dependencies:
<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>kov</groupId>
<artifactId>etf-creator</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- to get html request for api -->
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>
<!-- for a fast way to read in a file -->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven -->
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Driver</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Also, for someone reason it will not install when it was before. I get an error "Source option 5 is no longer supported. Use 7 or later."
I am new to Eclipse and making executable jar files on Eclipse, so I appreciate the help.
I figured it out. I needed more memory when running it.
I ran it with this and works faster now:
java -Xms512m -Xmx1024m -jar mainProgram.jar
I am trying to use NATTY by including it as a maven dependency. I just did the Hello, World Maven tutorial -- but am otherwise unfamiliar with Maven. The instructions on the natty site say to include natty as a dependency in the pom.xml. I have done so like this
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.joestelmach</groupId>
<artifactId>natty</artifactId>
<version>0.9</version>
</dependency>
</dependencies>
</project>
I then run $mvn package and the project builds successfully. I see one jar file in my /target: my-app-1.0-SNAPSHOT.jar so I assume that the natty dependencies are baked into that jar.
To test, I create a simple class in a file called Temporary.java to hold the natty demo code:
import com.joestelmach.natty.*;
import com.joestelmach.natty.generated.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class Temporary{
public static void main(String [] args) {
Parser parser = new Parser();
List groups = parser.parse("the day before next thursday");
for (DateGroup group : groups) {
List dates = group.getDates();
int line = group.getLine();
int column = group.getPosition();
String matchingValue = group.getText();
String syntaxTree = group.getSyntaxTree().toStringTree();
Map parseMap = group.getParseLocations();
boolean isRecurreing = group.isRecurring();
Date recursUntil = group.getRecursUntil();
}
}
}
But when I run $ javac -cp target/my-app-1.0-SNAPSHOT.jar Temporary.java I get
Temporary.java:1: error: package com.joestelmach.natty does not exist
import com.joestelmach.natty.*;
What am I doing wrong?
You need to make sure that when you package your jar using maven that your dependencies are included.
I believe you need to add this to your pom.xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Make sure when running it you use the .jar that has the "jar-with-dependencies.jar" at the end. This will ensure that your natty dependency is included.
I'm Trying to execute an *.exe file after extracting it from the project resources to a local/temporary path. It is working fine when I run the code through NetBeans. The problem comes when I build it for an Standalone Jar. Simply doesn't run, is not extracting the resource and not executing it.
I omitted the instantiation for brevity.
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);
}
}
}
And this is how my pom.xml looks like, it just load the dependencies, and builds a jar with the libraries included within it:
<?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>
It works fine and no errors given When I execute it in Netbeans or through the commandline with:
java -cp myjar.jar com.jeflopo.Main
But It doesn't work when I doubleclick the .jar
I'm not an expert, could anyone help me to debug the error ?
I don't really know what I have done to make it work again. But It's working now.
I have executed it by command line, I have run the command "mvn package" and still It wasn't working launching by double click.
I just went to eat, and when I have returned I have tried again... And now it launch.
Ehm... #Sajan Chandran suggested to show the manifest file, but I think that since i'm not using maven-jar-plugin I haven't a manifest file. I'm just using maven-assembly-plugin to assemble all the dependencies in just one distributable archive.
Thanks anyway.
And excuse my grammar, english isn't my first lang...
Hello all I'm pretty new to using maven to build Java project so I'm sure the solution to my problem will be quite easy. I have installed the Eclipse Maven plugin and created a maven project. I now wish to package this project and distribute it as a runnable jar file. However when I run
mvn package
and then try to run the jar file I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: main/Formatter
The pom for this particular file looks like so:
<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>GROUP</groupId>
<artifactId>pcap.csv.xml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</project>
Any help would be greatly appreciated.
You need to specify the main class in the jar plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>main.Formatter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
If you want to distribute a single jar you need to use a single-jarifier (fatjar, jarjar, onejar, etc.)