How to setup LWJGL with Maven? - java

Im unable to successfully add proper dependencies for LWJGL in maven project. I have copied lwjgl dependency tempalte from maven repository, added it to my pom.xml and tried to run basic application from lwjgl.org, without success.
Below you have my pom.xml file, unfortunately when I try to run my first application i get:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Failed to locate library: lwjgl.dll
I found some sollution that I need to download all jars and attach them as jar library, so I did. File > Project Structure > Librarires > added folder which store all jars for LWJGL, unfortunately this is still not working.
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-glfw</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.lwjgl/lwjgl-opengl -->
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>java3d</groupId>
<artifactId>vecmath</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>

Please go to https://lwjgl.org/customize, select a variant ("Release" or "Early Access"), select "Mode" = "Maven", select all your needed/wanted modules and simply copy/paste the produced pom.xml snippet.
Note that the pom.xml snippet presented on the website is not a complete pom.xml. You still have to provide the surrounding <project> XML element and additional needed XML child elements.

I'm using the IntelliJ Idea IDE for this explanation.
Go to https://lwjgl.org/customize, select a version (Release, Stable, or Nightly), select "Mode" = "Maven". I personally chose "Getting Started" for the modules but you can choose base on your needs. Now press "copy too clipboard" to copy the pom.xml file.
Now open up IntelliJ and create a new project. Select Maven Project and follow the creation of the program. I called mine "maven-test". Copy and paste the script (pom.xml) from the lwjgl website that you have in your clipboard and past it before the </project>.
Also copy and paste this inside of the
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
This will prevent an error because of the default Maven compiler which is v1.5
Make a new java class under maven-test/src/main/java called Hello World and paste in the code found at https://lwjgl/guide
Finally press the Maven button on the right of the IntelliJ IDE Editor and press the refresh button that says "Re-import All Maven Projects". Right click insideHelloWorld.java and press run 'HelloWorld.main()'

Related

Problem with modules java "Module 'mp3player' reads package 'javafx.animation' from both 'javafx.graphics' and 'javafx.graphics'"

I tried to make mp3player in maven, then download dependency javafx-graphics from mvnrepos
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>14.0.1</version>
</dependency>
</dependencies>
And I have problem in module:
"Module 'mp3player' reads package 'javafx.animation' from both 'javafx.graphics' and 'javafx.graphics'"
I work on MacOs, InteliJ, can someone help me? Screenshots from InteliJ:
1
2
3
go to open module settings -> dependencies and check if you have the same dependencies as in the following image:
screenshot
otherwise go to file-> Invalidate Caches and restarting and click invalidate and restart

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64

I am trying setup TensorFlow Java application in Eclipse Oxygen (OS: Ubuntu Linux 16.x). I installed Tensorflow and followed the process mentioned in official documentation for Java (Maven Project) installation. I downloaded libtensorflow-1.3.0.jar, jni files and included in the build path. When I execute the program I get this following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64. See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md for possible solutions (such as building the library from source). Additional information on attempts to find the native library can be obtained by adding org.tensorflow.NativeLibrary.DEBUG=1 to the system properties of the JVM.
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.TensorFlow.init(TensorFlow.java:36)
at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:40)
at com.tensorflow.malwaredetection.App.main(App.java:13)
App.java
package com.tensorflow.malwaredetection;
import org.tensorflow.TensorFlow;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!"+ TensorFlow.version() );
}
}
POM.xml
<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>TensorFlow</groupId>
<artifactId>MalwareDetection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MalwareDetection</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>App</exec.mainClass>
<!-- The sample code requires at least JDK 1.7. -->
<!-- The maven compiler plugin defaults to a lower version -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
I got tired of this error and tried to do this in an old-fashioned way. Created App.java in a separate folder and included jar, jni files in the same directory. When I execute this from command line, I get different error
dev#ubuntu:~/Downloads$ javac -cp libtensorflow-1.3.0.jar Test1.java
dev#ubuntu:~/Downloads$ java -cp libtensorflow-1.3.0.jar:. -Djava.library.path=./jni Test1
Error: Could not find or load main class Test1
dev#ubuntu:~/Downloads$
I think you need to include jni library dependency in your pom.
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow_jni</artifactId>
<version>1.1.0</version>
</dependency>
When you have tensor flow dependency in your pom.xml file as below
<dependencies>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow</artifactId>
<version>1.14.0</version>
</dependency>
It will download required 3 libraries
tensorflow-1.14.0.jar
libtensorflow-1.14.0.jar
libtensorflow_jni-1.14.0.jar
So you have to manually extract the 3rd jar(libtensorflow_jni) and get your OS compatible file like for windows you have to copy the tensorflow_jni.dll file and paste in your project root directory
that will solve your problem.
For Linking error the JNI library which you have downloaded is
not compatible with OS you are running,
is not in PATH () or
with lesser probability TensorFlow bu which you should be able to confirm from TF triage.
For second could not find class
your PATH must be appended with "." so that it can find class in current folder from where it is running
Less probability is if class name/package is not same.
If you can post code for class will check

java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil

I am using Windows10, eclipse-neon with JDK1.8 version,
I am getting the following exception.
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.bytedeco.javacpp.Loader.load(Loader.java:385)
at org.bytedeco.javacpp.Loader.load(Loader.java:353)
at org.bytedeco.javacpp.avformat$AVFormatContext.<clinit>(avformat.java:2719)
at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:391)
at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:385)
at com.segment.processor.AudioMain.main(ApacheMathAudioMain.java:20)
Error getting static method ID of org/bytedeco/javacpp/Loader/putMemberOffset
here are the dependencies I am using in my pom.xml
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg</artifactId>
<version>3.0.2-1.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>3.1.0-1.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>0.10</version>
</dependency>
First problem
Your versions don't match. org.bytedeco.javacpp in version 0.10 is from Dec 2014, while all your other versions are from May 2016. You need to use version 1.2 of org.bytedeco.javacpp, or better yet, update all dependencies to the latest version.
You can see the versions here:
org.bytedeco.javacpp-presets » opencv
org.bytedeco.javacpp-presets » ffmpeg
org.bytedeco » javacv
org.bytedeco » javacpp
Second problem
You include the dependencies for Java code only, but you don't include the dependencies for native code (both opencv and ffmpeg are native libraries). You need to include opencv-platform and ffmpeg-platform instead:
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
<version>3.4.1-1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>3.4.2-1.4.1</version>
</dependency>
This will make Maven download and include opencv and ffmpeg libraries for Android, Linux, MacOS and Windows, both x86 and x64.
java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil
Actual cause of error is different versions of dependencies. That's why javacpp package failed during mapping of classes.
Follow these step to resolve this problem:
Download latest version of Javacv library package from here
Copy these three .jar files into libs folder
ffmpeg.jar
javacv.jar
javacpp.jar
Create jniLibs folder in app\src\main
Now, create four different folders for different architectures
arm64-v8a
armeabi
armeabi-v7a
x86
Change extension of these two files ffmpeg-android-arm.jar, ffmpeg-android-x86.jar to .zip then unzip both folders and Copy .so files for each architecture and paste in its respected directory. Your resultant directory should be look like this
Add .jar dependencies in your gradle file as follows:
implementation files('libs/ffmpeg.jar')
implementation files('libs/javacpp.jar')
implementation files('libs/javacv.jar')```
Thanks for reading :)
Try to update the dependencies with the latest versions and check whether class exists in that or not
https://mvnrepository.com/artifact/org.bytedeco/javacv/1.4.1
https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/ffmpeg/3.4.2-1.4.1
https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/opencv/3.4.1-1.4.1
This means that the class is there at compiletime but missing at runtime. You have a couple of options:
Always execute with a Maven plugin.
Include the library in the classpath when running.
Use the Maven Shade plugin to make an Uber-Jar which includes dependencies.
I hope this helps!

Weld OSGi + Apache Felix = can't find packages

I use Apache Felix and weld-osgi for a Java SE application. The problem is that in injected bean I use #ApplicationScoped from package javax.enterprise.context.ApplicationScoped. But there is no such package in weld-osgi-bundle-2.1.2.Final.
This package exist in weld-se but it's not in the OSGi bundle. How can I solve this problem?
I would try running the following dependency as separate bundle:
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.1-20130918</version>
</dependency>
(Maven Central link)
Be careful, you need version 1.1-20130918. Version 1.1 does not have OSGi headers in the MANIFEST.MF. You can unzip the jar and check the META-INF/MANIFEST.MF file for OSGi headers like Bundle-ManifestVersion and Bundle-SymbolicName. You can also check here the required packages of that bundle, it's in the Import-Packages header.
How to figure out
Check the dependencies of weld-osgi-bundle on Maven Central (or in its pom.xml). It contains the following:
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-api</artifactId>
</dependency>
This weld-api refers to the cdi-api above which contains the missing annotation:
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
</dependency>
Another way is pressing F3 (Open Declaration) in Eclipse while the cursor in the ApplicationScoped annotation then in the Project Explorer View enable the Link with Editor and it will show that ApplicationScoped.class is inside the cdi-api-1.1.jar.
Finding OSGi version of another jars
You probably need more bundles than this one (transitive dependencies or it was only the first one which stopped the installation).
Not all well-known jar has OSGi headers, like the following one:
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
In that case search for the group id on Maven Central. Two results which contain the javax.inject package and have OSGi headers:
org.glassfish.hk2.external
org.apache.servicemix.bundles
If you can't find anything you can convert any jar to OSGi bundle by hand. Actually, you can do this with the weld-se.jar but installing dependencies separately looks cleaner.

Maven plugin builds but can't execute due to java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

I am using the maven-jspc-plugin in my pom.xml.
When i try to execute the jsp-compile goal (which executes the plugin) I get:
Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.apache.juli.logging.Slf4jLog.<init>(Slf4jLog.java:29)
at org.apache.juli.logging.LogFactory.getLog(LogFactory.java:54)
at org.apache.juli.logging.LogFactory.getLog(LogFactory.java:35)
at org.apache.sling.scripting.jsp.jasper.compiler.OriginalTldLocationsCache.<init>(OriginalTldLocationsCache.java:81)
at org.apache.sling.maven.jspc.JspcMojo.initServletContext(JspcMojo.java:426)
I've tried downloading the (open) source for the maven-jspc-plugin and i am able to easily "mvn install" -- I don't get any build issues, however when i use that build in my project pom it still crashes and tells me it can't find LoggerFactory.
I've logged an issue with the Apache Sling project but am not making much headway.
https://issues.apache.org/jira/browse/SLING-2350
This link includes some more troubleshooting info as well as a simple maven project that uses the maven plugin. downloading the jspc-test.zip and "mvn install"ing will result in the error I've mentioned.
Also, i took a peak at the org.apache.juli pom.xml and it doesnt appear to list any dependencies at all.
Any thoughts on how to resolve would be appreciated.
Thanks!
Plugin dependencies are supplied in a different part of the POM:
<project>
<dependencies>
<!-- dependencies defined here don't get included for plugins -->
...
</dependencies>
<build>
<plugins>
<plugin>
.... jspc plugin section ....
<dependencies>
<dependency>
<!-- Try adding slf4j here --->
Though it does sounds like their POM is invalid if it doesn't already specify slf4j.

Categories