Running Java 9 jshell in Eclipse [duplicate] - java

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.

Related

Pi4j to use java with raspberry Pi not working

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

Start a JavaFX application with maven and Java 17

My System:
Has multiple jdks (1.8, 16, 17)
Has IntelliJ's Maven
Is a windows 10 machine
I want to start my JavaFX app with maven. For that I use the javafx:run button inside of the Plugins menu.
Then I get errors about my jdk, being on a too-low-level (It's telling me I try to execute my Main method with my jdk-8), which is not true, as every single jdk-specification is set to jdk-17.
Please don't send me any links about other posts. I have already tried those and the steps there did not work out for me.
This is the error I get
Main has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
Yes I already tried to change IntelliJs jdks, the JAVA_HOME variable and the run configs of maven itself
Sometimes I also get an error from maven, telling me there is no JavaFX version 17 in the maven repository (but the maven repo of my workplace). Is there a way to change my maven-repo to the default maven website?
This is my 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>groupId</groupId>
<artifactId>M226a_miniproject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>17</release>
<verbose>true</verbose>
<fork>true</fork>
<executable>C:/Users/[user.name]/.jdks/openjdk-17/bin/javac</executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>ch.package.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx</artifactId>
<version>17.0.0.1</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-graphics -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>17.0.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.0.1</version>
</dependency>
</dependencies>
</project>
Maybe somethings wrong with the <version>0.0.8</version> in the second plugin?
Also: The error about the JRE not recognizing the class file does make zero sense, as even my maven is using a custom path to my jdk-17.
Edit: Updated pom.xml
(Fixed the problem on my Ubuntu 20.04 machine, but the error was the same)
The problem was, that I only had downloaded a jdk-17 into IntelliJ.
I simply installed the jdk-17 by running sudo apt install openjdk-17-jdk, which installs the jdk and sudo update-alternatives --config java, which updates the intex of the newest jdk (i think).
The last step was to restart IntelliJ and I was good to go.

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.

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.

How to set up a minimal Maven pom.xml file for a Heroku worker process in Java?

First off: I'm not a Java coder. I'm new to the Java/Maven tool chain. We're using a Java library for a project which we want to launch as a Heroku background worker.
This project relies on two external libraries, the mongodb Java driver which is available through Maven's central repo, and another third party library. I've seen the Heroku article on "unmanaged dependencies", but something else appears missing, as I get an error like: Could not find the main class: com.company.myproject.MyApp Program will exit. when I try to run the app locally according to Heroku's instructions on "Getting Started with Java".
I noticed that their pom.xml file contains a Maven plugin maven-dependency-plugin to copy dependencies, and when I check my target/classes folder, I don't see any of the dependencies.
Heroku also publishes a guide on building background workers in Java. That pom.xml contains a build assembly plugin, which seems more complex.
I'm a bit lost in all this ceremony (especially coming from Rails), and I'd like to stat with the simplest possible pom.xml to get this running. Is there a Maven archetype file for Java workers on Heroku? I'm also using NetBeans as IDE, and it would be great to use the IDE tools for this, if available, but it's a secondary priority.
Below my pom.xml so far:
<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.myproject</groupId>
<artifactId>myproject</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>myproject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.thirdparty</groupId>
<artifactId>thirdparty</artifactId>
<version>0.2.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>project-local</id>
<name>Project-local Repo</name>
<url>file:${project.basedir}/repo</url>
</repository>
</repositories>
</project>
You definitely need to use the maven-dependency-plugin to copy all of the dependencies into the target/dependency directory:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then your Procfile needs to include those dependencies in the classpath:
foo: java -cp target/classes:target/dependency/* com.myproject.Main
Where com.myproject.Main is the class name of the Java class you want to run (which must contain a public static void main method. Note that this also adds the Java classes which are compiled from src/main/java into the target/classes dir.

Categories

Resources