Pi4j to use java with raspberry Pi not working - java

I am honestly about to just give up, i've tried so many different possibilities, for multiple weeks now, almost a month, of multiple problems.
I am a new-ish programmer, especially with java, but i have a good understanding about java
I am able to create a maven project no problem, i have no problems with the structure of java itself, but i don't fully understand the pom.xml.
The file compiles just fine, but when i go to start it with java -jar (filename), i get the following output;
Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/Pi4J
at com.pi.rasberri.Main.main(Main.java:16)
Caused by: java.lang.ClassNotFoundException: com.pi4j.Pi4J
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 1 more
Heres my pom that i have figuratively almost dismembered;
(The long links)
<modelVersion>4.0.0</modelVersion>
<groupId>com.pi</groupId>
<artifactId>Rasberri</artifactId>
<version>3.6.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-raspberrypi</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-pigpio</artifactId>
<version>2.0</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId>
maven-jar-plugin 3.1.0
true
com.pi.rasberri.Main
lib/
true
true
com.pi.rasberri.Main
false
true
And my code, which is com.pi.rasberri.Main
package com.pi.rasberri;
import com.pi4j.Pi4J;
import com.pi4j.io.gpio.digital.DigitalOutput;
import com.pi4j.io.gpio.digital.DigitalState;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
private static final int PIN_LED = 6;
public static void main(String[] args) throws Exception{
var pi4j = Pi4J.newAutoContext();
int x = 0;
var ledConfig = DigitalOutput.newConfigBuilder(pi4j)
.id("led")
.name("LED Flasher")
.address(PIN_LED)
.shutdown(DigitalState.LOW)
.initial(DigitalState.LOW)
.provider("pigpio-digital-output");
var led = pi4j.create(ledConfig);
while(x != 5){
led.high();
sleep(1000);
led.low();
sleep(500);
x++;
}
}
static void sleep(int z){
try {
Thread.sleep(z);
} catch (InterruptedException ex) {
System.out.println("Thread.sleep went fucky wucky");
}
}
}
I would appreciate anything that can even direct me somewhere, because i am quite lost at this point, and of course, if i figure out the answer, i will let everyone know!
Thanks in advance
**UPDATE**
Thank you, tgdavies, the article you linked
https://stackoverflow.com/a/574650/17644313 was the answer to that
specific problem!**
Just in case anyone else has the same chain of issues as me;
But with that being said, i ran directly into another error;
[main] INFO com.pi4j.Pi4J - New auto context
[main] INFO com.pi4j.Pi4J - New context builder
[main] INFO com.pi4j.platform.impl.DefaultRuntimePlatforms - adding platform to managed platform map [id=raspberrypi; name=RaspberryPi Platform; priority=5; class=com.pi4j.plugin.raspberrypi.platform.RaspberryPiPlatform]
Exception in thread "main" com.pi4j.provider.exception.ProviderNotFoundException: Pi4J provider [pigpio-digital-output] could not be found. Please include this 'provider' JAR in the classpath.
at com.pi4j.provider.impl.DefaultRuntimeProviders.get(DefaultRuntimeProviders.java:238)
at com.pi4j.provider.impl.DefaultProviders.get(DefaultProviders.java:147)
at com.pi4j.provider.Providers.get(Providers.java:253)
at com.pi4j.context.Context.create(Context.java:316)
at com.pi4j.internal.IOCreator.create(IOCreator.java:58)
at com.pi4j.internal.IOCreator.create(IOCreator.java:96)
at com.pi4j.internal.IOCreator.create(IOCreator.java:176)
at com.pi.rasberri.Main.main(Main.java:27)
I cleaned the pom.xml a littlebit;
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-raspberrypi</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-pigpio</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<plugins> <!--Package all libraries classes into one runnable jar -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>com.pi.rasberri.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
At this point, i know of atleast 2 possibilities,
The code just needs to be run on the raspberry pi
I have entered the classpaths incorrectly
But i honestly dont really know as to where to start troubleshooting
I've tried a few things, one of which being to try another method of compiling the files,
but the other method just resulted in a even longer string of errors.
What should i try next?
Thanks in advance!
And thanks to anyone who has suggested troubleshooting steps thus far!
Oh, and the target is to hopefully just create a singular jar file to execute on the raspberry pi
update
I tried to run it on the Raspberri pi, and it resulted in the same error so it's not that

I do not have a Raspberry Pi to do final testing but I think I found a work-around.
Using this answer I managed to get 2021-10-30-raspios-bullseye-armhf-lite.img running in Docker. I built a project using your code and copied all files needed and tried to start it (mypiapp is my jar containing your main-class):
java -cp mypiapp-0.0.1-SNAPSHOT.jar:lib/pi4j-core-2.1.1.jar:lib/pi4j-library-pigpio-2.1.1.jar:lib/pi4j-plugin-pigpio-2.1.1.jar:lib/pi4j-plugin-raspberrypi-2.1.1.jar:lib/slf4j-api-1.7.32.jar:lib/slf4j-simple-1.7.32.jar com.github.fwi.mypiapp.MyPiApp
which then gave me the error:
[main] ERROR com.pi4j.library.pigpio.util.NativeLibraryLoader - Unable to load [libpi4j-pigpio.so] using path: [/lib/armhf/libpi4j-pigpio.so]
java.lang.UnsatisfiedLinkError: /tmp/libpi4j-pigpio1770932771276400506.so: libpigpio.so.1: cannot open shared object file: No such file or directory
which is a bummer. But at least there was no ProviderNotFoundException (which I also could reproduce using the "fat jar with dependencies"). The long java-command appears to prevent that exception from happening.
And I could improve the situation somewhat by running (in Raspbian)
apt install pigpio
and now the long java-command shows:
[main] WARN com.pi4j.library.pigpio.impl.PiGpioNativeImpl - PIGPIO ERROR: PI_INIT_FAILED; pigpio initialisation failed
which is to be expected when running in a Docker container. But at least the native library was found and loaded.
Now for the work-around which might solve the "fat jar" problems. We are gonna borrow some Maven setup and code from Spring. Spring can also build fat-jars and does it a bit more complicated but also better. The pom now looks like (update where needed with your project names):
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
</parent>
<groupId>com.github.fwi</groupId>
<artifactId>mypiapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
<start.class>com.github.fwi.mypiapp.MyPiApp</start.class>
<pi4j.version>2.1.1</pi4j.version>
</properties>
<dependencies>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-raspberrypi</artifactId>
<version>${pi4j.version}</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-pigpio</artifactId>
<version>${pi4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start.class}</mainClass>
<layout>ZIP</layout>
<executable>false</executable>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Simply run mvn clean package to build the fat-jar. Run it using java -jar mypiapp-fat.jar In my case, it gave the exact same error as when running with the long java-command (i.e. there was no ProviderNotFoundException). So I think this could work on a real Raspberry-Pi (the apt install pigpio might still be required though).
Some notes:
the Spring parent sets some good Maven defaults and also sets good defaults for the spring-boot-maven-plugin
Java version MUST be set using the java.version property.
update start.class with the name of the class containing your main-method.
the 3 dependencies in the pom.xml will pull in all other dependencies (dependencies of dependencies)
Spring documentation references: packaging and nested-jars launcher options.

Thanks tgdavies, MadProgrammer, and khmarbaise for the answers, the fix was to create a fat jar, which is basically a jar file that contains all the dependencies in one file, example can be found in the original question/comments
UPDATE!!
Pi4j has posted instructions for the V2 fat jar on their website!
I tried it and it works perfectly now
https://pi4j.com/getting-started/minimal-example-application-fatjar/

I faced the same issue working with Raspberry P4, java 11 and pi4j v2. I solved it downloading pigpio library directly from the raspberry
sudo apt-get install pigpio
After the library was installed, the jar worked perfecrly.
Remember also to run your program with sudo if you are not root user

Related

NoClassDefFoundError - missing dependencies in the classpath

My problem
I have written some java-code. The code runs flawlessly in intellij.But when I run it as a .jar file (i.e., the command java -jar app.jar), I get the error message:
Unable to initialize main class RSocketClient.Client
Caused by: java.lang.NoClassDefFoundError: io/rsocket/transport/ClientTransport
My Research
I have been searching about the NoClassDefFoundError on google and stackoverflow and have found that the error arises when dependencies are missing. The solution seems to be that I need to add dependencies to the classpath or maven repository. But I don't know how to do this (my pom.xml is presented below).
My Question
All I want to do is to turn my program into a .jar file and run it through my terminal (Windows10 OS). I appreciate all the help that I can get.
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>org.example</groupId>
<artifactId>RSocketSample</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>RSocketClient.Client</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.rsocket</groupId>
<artifactId>rsocket-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.rsocket</groupId>
<artifactId>rsocket-transport-netty</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
</project>
It looks like your jar doesn't contain classes of RSocket itself, and its fine.
Jar created by maven contains only your classes (in your module).
The jar of rsocket (as well as other jars that you're using,like SLF4J stuff for example) reside in the corresponding jars prepared by the maintainers of these third-parties.
IntelliJ "sees" the whole classpath (that includes all the dependencies) and runs your application with all this classpath.
Probably what you should do is create a jar including dependencies:
See This SO thread
Another option is keeping your jar "as is" but requiring that it will be run with all the dependencies in the classpath, but it looks like its not what you're trying to do, this way is used when you're developing a library and not a standalone application.

How to use FXCanvas with SWT Java 8

I am trying to integrate JavaFX inside of an SWT application using FXCanvas. For reference, I am following this oracle guide
Within my IDE this chunk of code displays an error
/* Create an FXCanvas */
final FXCanvas fxCanvas = new FXCanvas(shell, SWT.NONE) {
#Override
public Point computeSize(int wHint, int hHint, boolean changed) {
getScene().getWindow().sizeToScene();
int width = (int) getScene().getWidth();
int height = (int) getScene().getHeight();
return new Point(width, height);
}
};
Error:
(yes, I have imported the correct Point class: org.eclipse.swt.graphics.Point):
'computeSize(int, int, boolean)' in 'Anonymous class derived from javafx.embed.swt.FXCanvas' clashes with 'computeSize(int, int, boolean)' in 'javafx.embed.swt.FXCanvas'; attempting to use incompatible return type
However, I don't think this is the root cause... Because when I try to build the project (maven) I get this error:
package javafx.embed.swt does not exist. Which I believe is the true issue.
So after doing some research I have checked a few things, firstly, the jfxswt jar looks like it should be accessible:
and if I open the JAR, you can see FXCanvas is there:
I even tried adding the JAR manually as a library to my module, still doesn't work.
Here is my pom.xml, (i've intentionally anonymized certain info)
I will also mention that I have tried this in a fresh project without any maven dependencies, and just adding swt and such as libraries manually with the same error.
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</parent>
<artifactId></artifactId>
<version>2.0.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name></name>
<description></description>
<dependencies>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-engine</artifactId>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-core</artifactId>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-ui-swt</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>jface</artifactId>
<version>3.3.0-I20070606-0010</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
Am I missing something, any ideas?
You will need to add the jfxswt.jar file to your classpath for compile and for execution.
This can be done by using the system scope as that jar file is part of your JDK under the jre/lib directory.
<dependency>
<!-- GroupId/ArtifactId/Version doesn't matter unless it clashes. -->
<groupId>jfxswt</groupId>
<artifactId>jfxswt</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/lib/jfxswt.jar</systemPath>
</dependency>
This will instruct maven to add the jre/lib/jfxswt.jar to the classpath. This could cause an issue if someone uses different JDK what has that jar in other places but for Java 8 you should be okay.
You have to add the jfxswt.jar to your classpath when you execute your application.
In maven you can use the exec plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>twobuttons.TwoButtons</mainClass>
<additionalClasspathElements>
<!-- The run environment must configure the system jar. -->
<element>${java.home}/lib/jfxswt.jar</element>
</additionalClasspathElements>
</configuration>
</plugin>
Notes:
The system scope is deprecated in maven in favor of installing files to repositories. The solution above works fine at the moment.
The content of the jfxswt.jar can be part of some SWT libraries unfortunately I am not familiar with SWT. If you can find that jar in a Maven repo than just include that instead of messing with the system scope.
The twobuttons.TwoButtons class is from the tutorial you mentioned.

Running Java 9 jshell in Eclipse [duplicate]

As Java 9 introduced the concept of JShell which enables us to write code without creating a class and a method, is it possible to use this feature of Java 9 in eclipse ?
You can use the TM Terminal to run JShell in Eclipse:
If necessary, install TM Terminal (contained only in some Eclipse packages)
Open a 'Terminal' view in Eclipse: Window > Show View > Other...: Terminal > Terminal
Launch a new Local Terminal
Run JShell, e. g. on Windows type "C:\Program Files\Java\jdk-9\bin\jshell" -v followed by Enter
Alternatively, you can use a Scrapbook Page, a built-in feature of the Eclipse Java IDE and which also works with older Java versions. You will have code completion and you can use Java classes of your project:
If this is not a feature ask for Eclipse, a very basic stub that you can come up with is:
public static void main(String[] args) throws Exception {
jdk.jshell.tool.JavaShellToolBuilder.builder().run();
}
When you execute this, you can further use your debug console as JShell in your IDE.
Sample screenshot:
If you like to use JShell (from Eclipse or from a Terminal) to try out code, a very nice option is to use the Maven JShell plugin and just run mvn from a corresponding (dummy) project (in Eclipse: Right-Click on the Project and Run As -> Maven build).
In that case JShell knows all the libraries specified in the dependencies of the project.
I am use this here: http://www.finmath.net/finmath-experiments/montecarlo-blackscholes/
A small pom.xml using some libraries (JavaFX, Apache commons, finmath lib) could look 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.finmath</groupId>
<artifactId>finmath-experiments</artifactId>
<version>0.1.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean install jshell:run</defaultGoal>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.johnpoth</groupId>
<artifactId>jshell-maven-plugin</artifactId>
<version>1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- Java FX -->
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-base -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>11</version>
</dependency>
<!-- finmath-lib -->
<dependency>
<groupId>net.finmath</groupId>
<artifactId>finmath-lib</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>net.finmath</groupId>
<artifactId>finmath-lib-plot-extensions</artifactId>
<version>0.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
</dependencies>
</project>
Note: Personally I prefer running this from a Terminal in macOS, since JShell supports "TAB-auto-completion" there, which appears to be missing when running from Eclipse.
There are multiple ways to do this as explained in other answers. But I would like to tell you a plugin which will provide more feature than just starting a normal JShell from Eclipse.
Check this Eclipse plugin QuickShell
This plugin will start JShell in Eclipse terminal. Like this:
You can also select your existing java source code and run it as a JShell script. For example :
.jsh and .jpage files can be run from Eclipse directly.
PS: I am author of this plugin.

Is it possible to use sysout without class and main method in Eclipse IDE using Java 9?

As Java 9 introduced the concept of JShell which enables us to write code without creating a class and a method, is it possible to use this feature of Java 9 in eclipse ?
You can use the TM Terminal to run JShell in Eclipse:
If necessary, install TM Terminal (contained only in some Eclipse packages)
Open a 'Terminal' view in Eclipse: Window > Show View > Other...: Terminal > Terminal
Launch a new Local Terminal
Run JShell, e. g. on Windows type "C:\Program Files\Java\jdk-9\bin\jshell" -v followed by Enter
Alternatively, you can use a Scrapbook Page, a built-in feature of the Eclipse Java IDE and which also works with older Java versions. You will have code completion and you can use Java classes of your project:
If this is not a feature ask for Eclipse, a very basic stub that you can come up with is:
public static void main(String[] args) throws Exception {
jdk.jshell.tool.JavaShellToolBuilder.builder().run();
}
When you execute this, you can further use your debug console as JShell in your IDE.
Sample screenshot:
If you like to use JShell (from Eclipse or from a Terminal) to try out code, a very nice option is to use the Maven JShell plugin and just run mvn from a corresponding (dummy) project (in Eclipse: Right-Click on the Project and Run As -> Maven build).
In that case JShell knows all the libraries specified in the dependencies of the project.
I am use this here: http://www.finmath.net/finmath-experiments/montecarlo-blackscholes/
A small pom.xml using some libraries (JavaFX, Apache commons, finmath lib) could look 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.finmath</groupId>
<artifactId>finmath-experiments</artifactId>
<version>0.1.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean install jshell:run</defaultGoal>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.johnpoth</groupId>
<artifactId>jshell-maven-plugin</artifactId>
<version>1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- Java FX -->
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-base -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>11</version>
</dependency>
<!-- finmath-lib -->
<dependency>
<groupId>net.finmath</groupId>
<artifactId>finmath-lib</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>net.finmath</groupId>
<artifactId>finmath-lib-plot-extensions</artifactId>
<version>0.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
</dependencies>
</project>
Note: Personally I prefer running this from a Terminal in macOS, since JShell supports "TAB-auto-completion" there, which appears to be missing when running from Eclipse.
There are multiple ways to do this as explained in other answers. But I would like to tell you a plugin which will provide more feature than just starting a normal JShell from Eclipse.
Check this Eclipse plugin QuickShell
This plugin will start JShell in Eclipse terminal. Like this:
You can also select your existing java source code and run it as a JShell script. For example :
.jsh and .jpage files can be run from Eclipse directly.
PS: I am author of this plugin.

Error Updating Maven Configuration

I downloaded this project(to convert docs to Pdf as the project name say) https://github.com/yeokm1/docs-to-pdf-converter on github then I tried to import in Eclipse but something is wrong. I got this error.
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6
I also have errors(I have a red cross on this file) in my pom.xml but this is my first project with maven, I'm not able to detect the problem. Here the code:
<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>docs-to-pdf-converter</groupId>
<artifactId>docs-to-pdf-converter</artifactId>
<version>1.8</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
<version>2.32</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.odftoolkit.odfdom.converter.pdf</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
</project>
I tried to see many other questions with a lot of solutions, but no one worked for me.
1) I tried to delete the .m2 folder : Not Worked
2) Try to make right click on the project and then Maven-Update Project: same error. Not Worked
3)I tried to go to WINDOW-PREFERENCE-MAVEN-INSTALLATION and then I changed Embedded with the folder that contains Maven, in my case C:\Maven Not Worked
4)I tried to go to WINDOW-PREFERENCE-JAVA-INSTALLED JREs where at the begin I just had C:\Program Files\Java\jre1.8.0_65, then I have read somewhere that I need Jdk 7, so I downloaded and install it but it doesn't work too. So, Now I have these two C:\Program Files\Java\jdk1.7.0_79 and C:\Program Files\Java\jre1.8.0_65
I also post a picture( I have not enough reputation to post two, in ordert to see also the Path Environment Variable) about the Environment variable, maybe I don't see something obvious. enter image description here
I'm sorry in advance because it could be a very easy question, but as I said above this is my first project with maven/eclipse and I'm not expert in this field.
I downloaded this project, and it compiled fine using Eclipse Mars.
It sounds like your Maven installation does not know how to go out to the internet to find artifacts. Could there be a firewall in the way?
Also, in Eclipse go to Window/Preferences/Maven/User settings. Make sure the settings.xml is the same one as used by your external Maven installation (assuming you are using an external Maven engine). For instance, I have set both the global and user config files to C:\development\tools64\apache-maven-3.3.3\conf\settings.xml
I checked my settings.xml, and there is no configuration that points to the maven central repository - Maven 3 must just know where to find it.
On the first compile, you should see lots of info messages saying that Maven is downloading artifacts. The next time you compile, these messages will not appear, as the dependencies are now cached in your local repository.

Categories

Resources