Since JDK 11, the java command can launch a java source code file, i.e. no need to first compile your java source code. Here is my java source code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class HelloJavaFxWorld extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("Hello JavaFX World");
Pane root = new Pane(label);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I save this code as file HelloJavaFxWorld.java.
I open a command prompt window and enter the following command.
java HelloJavaFxWorld.java
This is (part of) the output.
HelloJavaFxWorld.java:1: error: package javafx.application does not exist
import javafx.application.Application;
How can I run a JavaFX source code file?
Since JDK 11 is modular, you need to add the JavaFX modules. Try the following.
java -p "path\to\javafx.graphics.jar;path\to\javafx.base.jar;path\to\javafx.controls.jar" --add-modules javafx.graphics,javafx.controls HelloJavaFxWorld.java
Replace path\to with the actual path to the JAR files. For example on my Windows 10 machine I have installed JDK 16.0.1 so I am using JavaFX 16 and have placed the [JavaFX] JAR files in this folder:
C:\Program Files\Java\javafx-sdk-16\lib
So my actual command for launching the JavaFX source code file is:
java -p "C:\Program Files\Java\javafx-sdk-16\lib\javafx.graphics.jar;C:\Program Files\Java\javafx-sdk-16\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-16\lib\javafx.controls.jar" --add-modules javafx.graphics,javafx.controls HelloJavaFxWorld.java
Note that instead of -p, you can use --module-path. Then the command becomes:
java --module-path "C:\Program Files\Java\javafx-sdk-16\lib" --add-modules javafx.graphics,javafx.controls HelloJavaFxWorld.java
Note that I enter that command from the folder containing the java source code file.
The above command may result in the following exception.
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: HelloJavaFxWorld
at javafx.graphics/javafx.application.Application.launch(Application.java:310)
at HelloJavaFxWorld.main(HelloJavaFxWorld.java:19)
Caused by: java.lang.ClassNotFoundException: HelloJavaFxWorld
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:466)
at javafx.graphics/javafx.application.Application.launch(Application.java:298)
at HelloJavaFxWorld.main(HelloJavaFxWorld.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:415)
at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:192)
at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)
This is because, when launching a java source code file, the source code is compiled and the compiled class is stored in memory, i.e. no .class file is created. Method launch(String...), in class javafx.application.Application, calls method forName, in class java.lang.Class in order to load the JavaFX application class. Since there is no HelloJavaFxWorld.class file, method forName throws ClassNotFoundException.
In order to fix that, simply change your java source code to call the other launch method. In other words, change method main to the following.
public static void main(String[] args) {
launch(HelloJavaFxWorld.class, args);
}
Now, when I enter the above java command, I get the following window.
Related
i'm trying run a project with this architecture...
And, when i press "Run" button the code gives me this error...
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0x709a3ba3) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module #0x709a3ba3
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2135)
at ui.Main.start(Main.java:32)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Exception running application ui.Main
My code of my Main...
package ui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import model.FIBAManager;
import java.io.IOException;
public class Main extends Application {
private static final String FOLDER = "fxml/";
private FIBAManager manager;
private MainGUIController MGC;
private EmergentGUIController EGC;
public Main() throws IOException {
manager = new FIBAManager();
EGC = new EmergentGUIController(manager);
MGC = new MainGUIController(manager, EGC);
}
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage window) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(FOLDER + "MainWindow.fxml"));
fxmlLoader.setController(MGC);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root, null);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
window.setScene(scene);
window.setTitle("");
window.show();
}
}
And i have this run cofiguration:
--module-path "C:\Users\Giova\Downloads\javafx-sdk-17.0.0.1\lib" --add-modules=javafx.controls
I need help to solve this problem and run the project please
You are using fxml so you need to add the fxml module, otherwise it will not be visible for use:
--add-modules javafx.controls,javafx.fxml
See the openjfx.io documentation titled Run HelloWorld using JavaFX SDK.
Example execution command for Linux from that source (modified to add the javafx.fxml module):
java --module-path $PATH_TO_FX --add-modules javafx.controls,javafx.fxml HelloFX
Alternately, you can define a module-info.java file which requires javafx.fxml and any other modules required for your application. See: Understanding Java 9 modules.
module <your module name> {
requires javafx.controls;
requires javafx.fxml;
opens <your package> to javafx.fxml;
exports <your package>;
}
Substituting your info for:
module <your module name> any name you want, but best to put it as the name of the primary package of your app, e.g. com.example.javafx.myapppackage. Follow Java 9 module naming conventions.
opens <your package> to javafx.fxml a package which uses fxml (opens is required to allow the fxml package to reflect on your code).
exports <your package> a package you want to make visible (via export) to users of your application's module code; e.g. the package which contains the class in your appliction with a main(args) method.
Or, you can ignore the module system using the hack provided in mipa's answer, as mipa does have a very good point about the additional complexity involved in using the module system. It is not a supported configuration by the JavaFX developers, but will likely work OK for your app.
Just append this line to the file containing your Main class.
class MainLauncher {public static void main(String[] args) {Main.main(args);}}
Then put all your dependencies on the classpath (also JavaFX) and in your command line remove everything that is related to modules. Then launch the main from the MainLauncher class and be happy. You'll see a little warning. Just ignore it. This module stuff is simply unnecessarily complicated.
There is a jdk created by Liberica that has JavaFX integrated into it so you can compile with javac and run with java. Make sure you download the "Full" JDK. This eliminates a lot of headaches.
I received this error in my testing attempt of my template in java on netbeans in Ubuntu 12.04. What do I need to do from here based on the error information below the code. What is it saying that I should do to fix my problem.
Here is my code
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class again {
public static void main(String[] args) {
Mat m=Highgui.imread("/root/image.png",Highgui.CV_LOAD_IMAGE_OLOR);
}
}
run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_0(Ljava/lang/String;I)J
at org.opencv.highgui.Highgui.imread_0(Native Method)
at org.opencv.highgui.Highgui.imread(Highgui.java:296)
at again.main(again.java:22)
Java Result: 1
UPDATE:I changed the code and it said it compiled successfully but I still can't get it to show the image in a window. I also have another class thats suppose to be "interconnected" to show the picture but it doesn't.
public class again {
public static void main(String[] args) {
}
Mat m=Highgui.imread("/root/image.png",Highgui.CV_LOAD_IMAGE_COLOR);
LoadImage loadImage= new LoadImage("/root/image.png",m);
}
Here's the other class
import java.awt.*;
import javax.swing.*;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class LoadImage extends JFrame {
public static void main(String[] args) {
}
public LoadImage(String imgStr,Mat m)
{
Highgui.imwrite(imgStr,m);
JFrame frame = new JFrame("My GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
// Inserts the image icon
ImageIcon image = new ImageIcon(imgStr);
frame.setSize(image.getIconWidth()+10,image.getIconHeight()+35);
// Draw the Image data into the BufferedImage
JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
frame.getContentPane().add(label1);
frame.validate();
frame.setVisible(true);}
}
Sorry I couldn't get it to fit right, but I can't find out what version of java I am using or even worse I might not have java installed
root#ubuntu:/# sudo java –version
Exception in thread "main" java.lang.NoClassDefFoundError: –version
Caused by: java.lang.ClassNotFoundException: –version
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: –version. Program will exit.
The easiest solution seems to be to use the package of OpenCV provided by PatternDiscussing and use a maven project.
As described on their GitHub project page:
https://github.com/patternconsulting/opencv
I added:
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-7</version>
</dependency>
to my pom.xml file and this code before the main.
static {
nu.pattern.OpenCV.loadShared();
}
For an Ant project, which are just called Java Projects in Netbeans:
Download the jar itself from:
http://central.maven.org/maven2/nu/pattern/opencv/2.4.9-7/opencv-2.4.9-7.jar
Right-click Libraries under your project in the projects window, choose Add Jar/Folder, select the downloaded jar.
You also need to extract all the files from it with:
jar -xf ~/Downloads/opencv-2.4.9-7.jar
Instead of using nu.pattern.OpenCV.loadShared(); just call System.load() on the correct version of the shared library for your platform.
eg.
static {
System.load("/home/shackle/nu/pattern/opencv/linux/x86_32/libopencv_java249.so");
}
With either of those changes it worked for me.
You will need a modern version of the JDK.
To get the latest netbeans and the JDK on any platform:
Use the "Netbeans with JDK8" here:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
To setup ubuntu repositories to pull in JDK 8.
Follow the instructions here:
http://tecadmin.net/install-oracle-java-8-jdk-8-ubuntu-via-ppa/
Once that is done you may need to add that JDK as a platform and update the project settings to use the later JDK.
To add a platform:
Goto Tools-> Java Platforms menu in Netbeans and click the "Add Platform" button.
To update the project settings:
Select your project, right-click for properties, select libraries and select a different platform in the drop-down box labelled Java Platform.
I'm currently learning Java. My background is primarily in C#. I'm trying to do a basic "hello world". Currently, i've written the following code in IntelliJ:
import java.io.Console;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world.");
}
}
My app compiles successfully. However, when I attempt to run it, I get a runtime error that says:
Exception in thread "main" java.lang.ClassNotFoundException: com.company.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Process finished with exit code 1
I don't understand what's happening. Can someone please help me?
It seems like you told IntelliJ to call the main method of class com.company.Main. But IntelliJ cannot find such a class. IntelliJ searches for a file Main.class in a folder com/company
Probably you want to tell IntelliJ to run the main method of "HelloWorld" instead of "com.company.Main"...
Go into Edit Configurations (see screenshot) and update your Main class path to be that of the changed package name. In my case, package is "thread" and in there I have a Main class:
You need to specify the package inside your class like this
package com.company;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world.");
}
}
Check these steps in order :
1) Is your file name and your class name is same ?
2) If you work on command line write javac yourfilename.java
3) Then java yourfilename
First Set JDK PATH ..Then compile and run code
JDK Path Setting:
Follow the given steps below to set the PATH
After installing JDK, right click “My Computer” icon
Choose “Properties” from context menu
Click “Advanced” tab and the click ‘Environment Variable’
In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value. For example, if you have installed JDK in C drive then path may be in the form C:\Program Files\Java\jdk1.6.0_12\bin
You will have to give your own path in which you have installed JDK.
Open Command prompt window, and run your Java code.
Change your main class from com.company.Main to the class you declared
public class RunScript {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I created a Java project and a package called com.klong
Inside of the package I have one .java file with the above code in it.
I export it into a runnable jar file. Then I try to run the jar in a command line using this:
java test.jar
When I try that, I get the following error
C:\Users\IBM_ADMIN\Tracing stuff>java test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: test.jar
Caused by: java.lang.ClassNotFoundException: test.jar
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:653)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:619)
Could not find the main class: test.jar. Program will exit.
I've tinkered with exporting into a normal jar file and such. I've looked at other questions about this error. I've tried using commands such as set classpath=BLEH
This project is as simple as can be so hopefully you can help me figure out this pesky error!
You need to run java -jar test.jar, what you're currently doing is asking java to look for compiled classes with the name test.jar.
I am trying a very simple use of JavaFX using a simple set of lines of code which I got from another stackoverflow page (here). But, the problem is clearly not with that code but with something more fundamental in the build and run process.
Here is my code:
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
.
.
.
Media medMsg = new Media("msg.mp3");
MediaPlayer medplMsg = new MediaPlayer(medMsg);
medplMsg.play();
At first I couldn't get this to compile at all. Eventually I figured out that I needed to put -classpath c:\Program Files\Oracle\JavaFX 2.1 SDK\lib\rt\jfxrt.jar on my javac command line. (One obvious complex of questions here is: Why isn't it documented in any obvious place (1) that this is needed and (2) how exactly to figure out what the path to the JavaFX installation is?!)
But, when I run the code I get:
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/scene/media/Media
at progtest.main(progtest.java:120)
Caused by: java.lang.ClassNotFoundException: javafx.scene.media.Media
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
What does this mean? It looks like it doesn't know, at runtime, how to find the class javafx.scene.media.Media. But, my %CLASSPATH% variable definitely has "c:\Program Files\Oracle\JavaFX 2.1 SDK\lib\rt\jfxrt.jar" in it.
Any ideas? Thank you in advance!
This question somewhat duplicates compile javafx 2.0 manually.
This answer is specifically for JavaFX 2 versions before the release of Oracle Java 8. For Oracle JavaFX 8+, the JavaFX runtime is on the classpath, so you don't need to explicitly add it when compiling or JavaFX running applications.
Java includes JavaFX in jdk7u6 and above (for Windows and Linux) and jdk7u4 and above (for OSX).
Download and use jdk7u6+ and you won't need to specify the jfxrt.jar file in any classpath and all of your JavaFX related classpath issues should go away.
Here is a link to an early binary build of jdk7u6.
For JavaFX 2.1 on Windows you do need to include the jfxrt.jar lib in your classpath for compile (NetBeans will do this automatically if you use it's JavaFX platform settings) and (if you haven't packaged your app correctly using the javafxpackager or JavaFX ant tasks), also at runtime.
JavaFX 2.1 for Linux is a pre-release (in case you are using that). For the Linux pre-release you would only have to include jfxrt.jar in your classpath at both compile and runtime if the JavaFX runtime on Linux was not set up correctly.
Here is an example of a command line compilation and execution of a JavaFX program under windows.
Launch an editor:
C:\dev\test>notepad HelloWorld.java
Paste the following code and save it:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
stage.setScene(new Scene(new Label("Hello World")));
stage.show();
}
}
Compile and run it JavaFX 2.2:
C:\dev\test>javac -cp "\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" HelloWorld.java
C:\dev\test>java -cp ".;\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" HelloWorld
For Oracle Java 8+, the explicit JavaFX classpath specifier is not required:
C:\dev\test>javac HelloWorld.java
C:\dev\test>java HelloWorld
Note that usually rather than just compiling the code and running it, you would also package the code with javafxpackager or the javafx ant tasks. These tasks will embed a launcher class into your packaged app which will detect the version and location of the JavaFX runtime so that you don't need to specify the jfxrt.jar location unless you want to override the default location for the platform.
For Java 7 update 21, Windows XP SP3, I used
javac -cp "\Program Files\Java\jre7\lib\jfxrt.jar" HelloWorld.java
and
java -cp ".;\Program Files\Java\jre7\lib\jfxrt.jar" HelloWorld