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.
Related
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 ☺
If eclipse has a compiler (see Wikipedia article on IDEs, third line), itself why do I have to download the jdk for compiling .java files? Doesn't eclipse convert my file to bytecode?
Eclipse has its own compiler and it can work with JRE only. JRE is needed because Eclipse itself is run with java. However if we have JDK installed we can see Java platform class sources during develepment. Also Eclipse's Maven plugin uses JDK compiler.
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 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
I am working an application with JXL API and when i tried compiling using eclipse IDE, it's working fine and the same is not compiling when i am trying to compile in Command prompt and showing the below exception..
Extract.java:6: cannot access jxl.read.biff.BiffException bad class file: C:\Program Files\Java\jdk1.5.0_01\jre\lib\ext\jxl.jar(jxl/read/biff/BiffException.class)
class file has wrong version 50.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
import jxl.read.biff.BiffException;
^
1 error
EDIT:
I am able to executing using JDK 1.6. Since JDK 1.6 must also be compatible with lower versions, why doesn't it support the class files which were compiled in JDK 1.5.
The library you're using was compiled with Java 6
Your compiler is Java 5 that's why it doesn't understand that format.
To fix it you have to get a 1.5 version of the library or upgrade your compiler to 1.6 I suggest the later.
Per http://www.jnode.org/node/2140...
Submitted by Stephen Crawley on Fri, 11/30/2007 - 07:15.
I suspect that you are mixing code compiled with different versions of Java. Class file version 50.0 is used by Java 6.0, and 49.0 is used by Java 5.0.
Try doing a "build clean" to get rid of all existing class files, followed by a regular build.
JNode is being developed using Java 6.0 only. Last time I tried, it didn't build using Java 5.0 (aka 1.5). (It is a problem with the program that builds the JNode boot image.)
Try changing the builder in Eclipse. If you're using 3.4, it's Project - Properties - Java Compiler - Enable Project Specific Settings - Compiler Compliance Level = 1.6. You'll prolly also need to have JRE 1.6 installed, as well.
Check you class path in eclipse and make sure that its the same class path your compiling to in the command prompt, also check your library imports
It means that, you have compiled that class with Java 6 and trying to execute with Java 5.
Solution :
If your using ant, execute below steps on the project root directory
ant clean
ant deploy
If your using eclipse, just
clean the workspace(remove the class files which were compiled with Java6)
and build again
this could be that in you IDE you point to latest version of JDK but when you build your program outside the IDE(maybe with maven) your java_home is the older version to the one on your IDE.