I know that there's a lot of question related to this issue but none made sense for me. I built a Java Desktop application that adds products for sale by communicating with an API. I am using okhttp 3.9.0 to accomplish this task. The problem is that my app works just fine when I execute the Netbeans' "run project" command but when I use the "java -jar file.jar" command to run the app I get exceptions stating that okhttp3 classes weren't found. That's the first time I try to run a Java application outside of an IDE so I kind of lost.
I am running my app from:
C:\Users\Diego Alves\.m2\repository\com\mycompany\loja\1.0-SNAPSHOT
Also, something that bugs me is that when searching for okhttp3 I generally end up on an Android-related page. Isn't okhttp3 used for desktop apps?
You need to add dependencies to your jar(okhttp is one of them) so that the JVM can find them in your classpath. To do that, in pom.xml add the following plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>your.package.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Also make sure you have set <packaging>jar</packaging>
Then run the build (mvn clean install), it will generate a jar that can be executed successfully.
java -jar target/your_jar_name.jar
For OkHTTP there is no link with android, you can use it in any Java Application.
Related
When installing PDFBox with Maven, it places the libraries in the ~/.m2/repository directory.
My program complies fine with mvn package.
When I try to run it with
java -cp target/java-project-1.0-SNAPSHOT.jar com.example.sub.App
then I get
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
Should I also be specifying the libraries in ~/.m2/repository as part of the classpath? This seems a bit too tedious to do it this way. What is the recommended way to specify the classpath of my PDFBox library while using the library location(s) of Maven?
I wasn't able to find a nice solution with leaving the JAR files in ~/.m2, so the answer below is a workaround based on some other answers. I will be including more clarification though for those who are new to both PDFBox and maven as I am.
1) Add the following to your pom.xml file. If you already have a <build> and <plugins> section, just add the <plugin> section below to that. Otherwise you may need to add the whole code below within the <project> element:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>**REPLACE THIS WITH THE FULL URI OF YOUR MAIN CLASS**</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2) Make sure that you replace the text in the <mainClass> element to match the situation. For example, if your main() method is located in an App class in App.js, and your package name is com.example.sub. Then the above element should read:
<mainClass>com.example.sub.App</mainClass>
3) To compile your app, run mvn package
Note: I have seen some references online using mvn clean compile assembly:single instead of mvn package. I am not sure what the purpose of this is when mvn package seems to run just fine for me.
This will take your project and all of your dependencies and create a single JAR file in the target directory called something like this:
java-project-1.0-SNAPSHOT-jar-with-dependencies.jar
4) To run the project you can do this:
java -cp target/java-project-1.0-SNAPSHOT-jar-with-dependencies.jar com.example.sub.App
Make sure that you modify the above line to it your situation. In other words you may need to change both the name of the jar file and the name of the URI for your main class.
I am trying to create a .jar for my app. This app contains many things such as another .jar.
Using this plugin in my pom configuration :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.rilent.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
And I have this link to a local jar
...
<dependency>
<groupId>it.sauronsoftware.jave</groupId>
<artifactId>jave</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jave-1.0.2.jar</systemPath>
</dependency>
I am using this command to build my app which returns BUILD SUCCESS :
mvn clean compile assembly:single
but when I try to execute my program, It crashes at some point returning this error :
Exception in thread "main" java.lang.NoClassDefFoundError: it/sauronsoftware/jave/InputFormatException
( a class from the .jar)
I tried decompressing my main jar to see its content :
what is wrong with the way I am creating my jar?
edit #Thorbjørn Ravn Andersen
I tried this way too, I added this to my pom :
<!-- https://mvnrepository.com/artifact/com.jolira/onejar-maven-plugin -->
<dependency>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
</dependency>
then I m compiling with mvn compile and when I try to run the .jar I get this error
no main manifest attribute, in .\myapp-0.0.1-SNAPSHOT.jar
what am I doing wrong again ...?
Note : My jar also contains a .exe
The standard classloaders cannot load classes from jars inside jars. EXE files cannot be run from inside jars.
This does not mean that it is impossible, but a bit cumbersome and perhaps above your current skill level. You might find One Jar (http://one-jar.sourceforge.net/) interesting as it automates exactly this.
For a Maven project I have had good experience with creating a deployment structure and scripts with appassembler. http://www.mojohaus.org/appassembler/appassembler-maven-plugin/
I've spent the entirety of yesterday and the start of today trying to find out why Codenvy is using Java 8, but cannot find the JavaFX library that is included with it. I've looked through the files of the machine and cannot find it anywhere. I've also attempted to add it natively as a Source jar, but Codenvy seems to have removed support for this. On top of this, their tutorial page for adding source jars has been replaced with how to set up Che. I just use the Codenvy website and keep everything on the cloud.
So my problem is that I cannot get sound (MP3) to play. I tried the project on a workspace I set up in Codenvy previously, and it worked without an issue... yet a workspace made post Codenvy Beta release doesn't work, the JavaFX library just isn't there. The workspace from the older Codenvy doesn't have a stack (see below). Where as the newer projects do. I've tried workspaces with all the stacks that include Java, and still nothing. I did put the project into the old workspace and it worked flawlessly, but the old workspace doesn't have all the features of the newer ones, like ctrl+space for auto-complete, ect. I may aswell be using Notepad.
Is anyone else experiencing this, and is there a way to fix it?
Below is also my pom.xml so you can see the configuration.
<?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>TMMOJ</groupId>
<artifactId>TMMOJ</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<argLine>-Xmx1024m</argLine>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<compilerVersion>1.5</compilerVersion>
</configuration>
</plugin>
</plugins>
Managed to get this working by adding the jfxrt.jar from the JDK .tar.gz from the official java website to a lib folder, and adding it as a dependency (as described in one of the answers to this question JavaFX 2 as a Maven dependency). This solved the problem.
After building the Java program in NetBeans, I compressed the dist folder, placed the program in a USB. In another computer, after extracting all the files, I tried running the JAR file but a Window prompt said:
"Could not find the main class: logic.Main. Program will exit."
After researching and tried the solutions of similar problems (i.e. creating Manifest file, creating .bat file) but nothing works.
Then I ran it in command prompt and these were the results:
Are there 2 problems: could not find main class and that in the other computer, the Java is not updated? How to solve this?
It was actually able to find a logic.Main, but it wasnt able to load it because it was compiled with Java 8 and the user's machine is running an earlier version of Java. Compiling the file on an earlier version of Java or updating Java on the target machine will fix the issue.
There are multiple ways of creating executable jar.
In netbeans there is a option
Project Properties -> Build -> Packaging -> Build JAR after compiling
Maven Build can also be used for creating executable jar. Define main class in below maven plugin. Also you can select the compiler version to avoid major minor issue.
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.kulhade.elasticsearch.Indexer</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
I'm trying to remotely debug a jar file bundled with dependencies created with maven-assembly-plugin. But everything I do, and no matter how I call the debugger on the machine running the code (or on my local machine for testing) I always get a "Could not find main class". That's the message even if I specify the exact main class in the command line.
POM file:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.xxx.yyy.MyMainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
I'm creating the jar with dependencies with:
clean compiler:compile assembly:single
The debugging is started with:
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8800,server=y,suspend=y -classpath %classpath com.xxx.yyy.MyMainClass pubsubtest-0.1-jar-with-dependencies.jar
The application starts and says:
Listening for transport dt_socket at address: 8800
But when I connect to the port with Netbeans the issue is:
Error: Could not find or load main class com.xxx.yyy.MyMainClass
Am I missing something? Should I setup Netbeans somehow? In Netbeans I'm keeping the project open.
I followed this tutorial:
https://blogs.oracle.com/atishay/entry/use_netbeans_to_debug_a