I wanted to try and make a simple JavaFX app in sbt, but it seems sbt is unable to locate any of the javafx packages, giving me errors like
error: package javafx.application does not exist
error: package javafx.fxml does not exist
error: package javafx.scene does not exist
... and so on
And I find that odd given the fact that the javafx package is included in Java 7+ by default, so if anything, the packages SHOULD be available to the compiler, but it doesn't seem that way..
Any help?
ps: I'm not using any javafx related plugins, just pure sbt, and I'm trying to compile a Java project, not a Scala one. The project is set up to be compatible with Eclipse using sbteclipse
How to build something against JavaFX (in SBT or any other tool) depends a lot on your version of the JDK:
Using JDK 8
It all works out of the box: JavaFX is located in jre/lib/ext, which means it is on the default classpath of java and javac, and it should be available automatically both when compiling and running. That's the configuration #JacekLaskowski has in his answer.
This only works if you only target Java 8: JavaFX 8 is not available for Java 7, so compiling against it makes your app Java8-only (unless you make sure to only use things available in JavaFX 2.x, target jdk7 bytecode, package JavaFX 2.x with your app, etc.)
Using JDK 7u6+
The JavaFX SDK is distributed with the JDK, but it is not available automatically: it is not on the classpath of anything, you have to look for it in jre/lib and add it to the classpath by yourself. That's what some IDEs do automatically when they have JavaFX support.
SBT doesn't do that automatically for you. There is sbt-javafx that helps a little, but you still need to configure the location of jars, etc.
Using JDK 6 to JDK 7u5
You have to download the standalone version and add it to the classpath. The rest of the jdk7u6+ case above applies.
Finally, note that new features are added to JavaFX during the lifetime of Java 8, so building you app may require a specific version of JDK8 (this also happened a little in JDK7), and that's also one of the reasons you are supposed to package JavaFX with your application.
Basically as soon as you depend on JavaFX, you have to track the JDK and/or JavaFX itself as two unmanaged dependencies, with individual developers having to check versions and configure things.
That's odd as I'm new to JavaFX and have never worked with it before yet it works like a charm for me - at least I could import javafx.application package.
scala> import javafx.application
import javafx.application
You'd need to include more information about your environment. Mine's below.
> about
[info] This is sbt 0.13.5
[info] The current project is {file:/Users/jacek/sandbox/sbt-learning-space/}sbt-learning-space 1.0.0
[info] The current project is built against Scala 2.10.4
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, com.typesafe.sbteclipse.plugin.EclipsePlugin, net.virtualvoid.sbt.graph.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import javafx.application
import javafx.application
Related
I'm trying to compile a Java program using the Java 11 compiler from the command line on Arch Linux. The program compiles and works great from Eclipse IDE; however, when I try to compile it using the 'javac' command, it throws the following error:
javac MyApp.java
MyApp.java:3: error: package javafx.application does not exist
import javafx.application.Application;
^
MyApp.java:4: error: package javafx.stage does not exist
import javafx.stage.Stage;
Any help would be appreciated.
Regards
JavaFX is not part of the Java SDK anymore and needs to be installed separately (or you use Maven, Gradle or another build system). See the official documentation.
Build tools
Use a build tool such as Maven or Gradle to (a) download the desired version of the OpenJFX (JavaFX) libraries, and (b) include those libraries in your build, to be bundled inside your final JAR file.
JDK bundled with libraries
Or, use a JDK that includes the OpenJFX (JavaFX) libraries. Use such a JDK to compile. And ensure that your users have such a JDK installed on their computer.
This may work well in a controlled setting such as a corporate office, but may not be practical for distributing to the public.
At least two JDK vendors provide a variant of their distributions to include OpenJFX (JavaFX) libraries: Azul Systems, and BellSoft.
We want to use both SWT and JavaFX in an Eclipse plugin within our RCP application. Unfortunately, we're experiencing problems integrating SWT with JavaFX under Java 11. The build environment uses e(fx)clipse 3.5. We're developing against the Community edition of Azul Systems' "Zulu" JDK 11, bundled with Azul's version of OpenJFX.
Formerly, we developed under Java 8. At that point, our build used a compile-time class path referring to jfxswt.jar, which lived in the jre/lib directory of the JDK. We didn't use any special class path settings at run-time.
We are now trying to move to Java 11. There, this JAR has become javafx-swt.jar and lives in the lib directory of the JDK. It no longer seems to be enough to set the class path to refer to this JAR at compile time: it seems to be necessary to do so at run time too. If we don't do this, we get an error (java.lang.NoClassDefFoundError: javafx/embed/swt/FXCanvas).
As a test, we experimentally embedded javafx-swt.jar within the JAR implementing our Eclipse plugin. We then referred to it in that plugin's .classpath file, and the code worked as expected. Unfortunately, we can't embed the JAR this way for legal and other reasons.
A note on Java modules: javafx.swt does not show up in the output when we issue the --list-module command. We tried running the application using parameters -p /path/to/JDK/lib/javafx-swt.jar --add-modules javafx.swt, but this doesn't seem to solve the problem.
My question: Is there a way to set up the class or module path to allow our Eclipse plugin to find this library in the JRE? Any solution would have to work with whatever JRE the code happens to be run against (I think it is all right to assume lib/javafx-swt.jar will live in that JRE).
Would it help to use a separately-downloaded version of OpenJFX rather than the copy of OpenJFX in our JDK?
Very many thanks ☺
I would like to use jlink for creating self-contained application packages for all platforms (darwin, linux, windows) from Scala source code. It seems that jlink only works with new (relatively) jigsaw modules - so I need to package my code as a module. In Java world it seems to be easily achieved by placing special module-info.java file to the package that will become a module.
I tried to follow intuition and just placed this module-info.java into src/main/java/my.package.name/module-info.java. Though this doesn't work. It seems that scalac is trying to read module-info.java as usual Java file (which is not the case), hence the error
module-info.java:1:8: illegal start of type declaration
[error] module my.package.name {
[error] ^
What do I need to do to package my Scala code as a module?
Open JDK: 11
Scala: 2.12.4
SBT: 1.1.6
In general seems like that scala doesn't completely support Java9+, at least their compatibility notes read so.
As of Scala 2.12.6 and 2.11.12, JDK 9+ support is incomplete. Notably,
scalac will not enforce the restrictions of the Java Platform Module
System, which means that code that typechecks may incur linkage errors
at runtime. Scala 2.13.x will provide rudimentary support for this,
but likely only in nightlies built on Java 11.
You can follow Support features of JDK 9+ and Java 11 testing for further updates.
I recently went from dirty OS Windows 7 to best OS Arch Linux. After copying the stuff over, IntelliJ does not recognize some annotations:
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
I'm using Oracle JDK 1.8.0_92. On Windows I'm using 1.8.0.
On Project Structure -> Platform Settings -> SKDs -> Classpath Windows has the following additional jars:
sunmscapi.jar
access-bridge-64.jar
Apart from that I couldn't find any additional information. How can I make the annotations work?
Those annotations are not part of JDK 8. They are available in a separate JAR, and you need to add this JAR as a library to your project in order for the annotations to be resolved.
You can download the JAR, for example, here.
I'm using Intellij Idea 15.0.1 and have a Java project, it imports class of default package from the class of a named package.
Since it is allowed only in Java 1.3, I've set JDK 1.3 everywhere:
- Project Settings
- IDE Settings - Java Compiler
- Edit Configurations (Application)
But still when I make project it stops with "Cannot resolve symbol" error. Also making starts with the message:
Information:Using javac 1.8.0_51 to compile java sources
Why Idea still uses javac 1.8 and how to set java 1.3 for the whole project, including syntax highlight?
You need to install JDK 1.3, configure it in Project Structure | SDKs and select it as the project SDK in Project Structure | Project.