Compiling and running with JavaFX 2.1 - java

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

Related

How to launch JavaFX source code file from command line?

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.

How do I pass an additional jar do my java command when running application?

I have a java gradle projectA that references another java gradle projectB, that builds as a lib.
My gradle build configuration seems to be fine, since I can import and use classes from the other project, and it compiles. But when I try to run the application from the command line I get an error...
Exception in thread "main" java.lang.NoClassDefFoundError:
dawcore/SamplerInstrument at DawCLI.main(DawCLI.java:17) Caused by:
java.lang.ClassNotFoundException: dawcore.SamplerInstrument at
java.net.URLClassLoader.findClass(URLClassLoader.java:382) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
It seems to be complaining that it can't load the classes in the jar at runtime....which makes sense. But I don't know how to have it successfully load those classes.
my current run command that does not reference the jar, is as follows....
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/" DawCLI
Running this gives the initially mentioned error.
I then read the docs on java -cp argument. It says to provide additional classpath directories, to separate them by semicolon.
I susequently updated my run command to be as follows....
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/;../DawCore/build/libs/DawCore.jar" DawCLI
This gives me the following error....
Error: Could not find or load main class DawCLI
My main function is as follows..
import dawcore.*;
public class DawCLI {
public static void main (String [] args) throws IOException {
SamplerInstrument samplerkick = new SamplerInstrument();
}
}
According to the docs I seem to be doing this correctly, but am still getting errors. Any insight on this would be greatly appreciated!
Thanks
The solution was to replace the delimiting semi-colon with a colon instead. Even though the first line of the javadocs on -cp command indicate that you should use a semi-colon.
This could be because I am running on linux and those docs indicate Windows.
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

Eclipse No Class def found - Loadrunner lr.class

We wanted to run Junit Test in Loadrunner and below is the code written in eclipse:
When i run the code in eclipse i get NoClassDefFoundError Exception. From Loadrunner Vugen Folder I zipped the "lr.class" file and imported it in the eclipse Jar libraries.
import classes.lrapi.lr;
import org.junit.Test;
public class Jtst {
#Test
public void tst() {
System.out.println("Tst method");
try{
lr.start_transaction("T11");
//lr.start_transaction("T1");
System.out.print("Executing...");
lr.end_transaction("T1",lr.PASS);
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String args[]) throws ClassNotFoundException
{
System.out.println("Main");
Jtst j1 = new Jtst();
j1.tst();
}
}
When i run the program I get the Exception NoClassDefFoundError Please find below the error message.
java.lang.NoClassDefFoundError: lrapi/lr
at Jtst.tst(Jtst.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run
The easyest way to add a references to LoadRunner API is by using LoadRunner developer's add-in.
To use it (from LoadRunner user guide):
Make sure you have JDK 1.7 (JRE 7) on your machine. Go to oracle.com to check your version or download the required version. After you install it, open Eclipse and select Window > Preferences. Navigate to the Java > Installed JREs node. If jre7 is not in the Installed JREs list, click Add and use the wizard to add its folder (for example c:\Program Files\Java\jre7). In the Installed JREs list, click the check box by jre7 to instruct Eclipse to use this version.
Run the Eclipse Dev add-in, LREclipseIDEAddInDevSetup.exe, from the download/DVD folder: Additional Components\IDE Add-Ins Dev. After installing the Eclipse add-in, rebuild the plugin cache by running the following command line string: Eclipse.exe -clean.
In Eclipse, open your Selenium or JUnit test.
Code the test as you normally would in Eclipse.
Build your java classes.
Select Devops Vuser > Add LoadRunner API Reference to add the desired LoadRunner functions to your script as well as transactions, rendezvous points, and messages.

How to use Eclipse and Java for Mac 10.8.1

When I try to do a simple "Hello World" java application I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: /path/to/mainarg*
Caused by: java.lang.ClassNotFoundException: first.Skeleton
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I am running on Mac OS 10.8.1 and am running Java 1.6. I have checked my run configurations to make sure the main argument can be found. For the sake of simplicity I have also tried to use the default JRE of java 1.6 that is included with mac. The build configuration I have used is also correct and pointed to the right place.
When I followed this tutorial online, and executed the commands over the terminal I get the same error. What am I missing? I don't think it is an Eclipse problem anymore, but something bigger. Any advice would be greatly appreciated.
Here is the code I tried to run:
public class Test{
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String[]args){
System.out.println("Hello World");// prints Hello World
}
}
I put the code on my Desktop for simplicity. I went to /usr/tyler/desktop then made the script an executable with "javac Test.java", and when I tried to run it with "java Test.java" I then got the error.
Here are the other tutorials I have followed:
-Java Compilation error on Mac
-HelloWorld with Eclipse on Mac
-Along with many others from SO and elsewhere
EDIT:
Thank you for the help. The terminal issue has been resolved. However, if anyone has any advice on what could be going wrong with Eclipse, that would be appreciated.
You need to run ...
"java Test"
You cannot execute the java code, you need to execute the compiled class file. The java executable will look for a .class file with the name Test.
execute or run
java Test
instead of
java Test.java

Using a jar in a Java project?

I'm trying to use the public methods/classed from a project provided as a jar file (called Hello.jar for instance) wrapped in a package called hello.
package hello;
public class Hello
{
public static void main(String[] args)
{
coucou();
}
public static void coucou()
{
System.out.println("Hello there");
}
}
In a separate project called Tool, I want to be able to call the method Hello.coucou() so I wrote something like this:
import hello.*;
public class Tool
{
public static void main(String[] args)
{
System.out.println("main program running");
Hello.coucou();
}
}
and I compiled Tool.java with the following command (under linux):
$ javac Tool.java -classpath .:./extern/:
where Hello.jar is located in the folder ./extern
This seems to compile fine but when I launch it (i.e. java Tool), I get this:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
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)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
I am new to Java (C/C++ background) and I don't understand what I'm doing wrong.
Any ideas?
Cheers
David
Edit: I tried adding Hello.jar to the classpath on the command line, but I still get the same error:
$ javac Tool.java -classpath .:./extern/Hello.jar:
$ java Tool -classpath .:./extern/Hello.jar:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
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)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
You need the Hello.jar on the classpath when you run as well as when you compile.
Actually the trick was in the order of the arguments in the command line:
Is the -cp (or -classpath) is set last, then it doesn't work
java Tool -cp .:extern/Hello.jar
It has to be first like:
java -cp .:extern/Hello.jar Tool
!!!
Java uses dynamic late binding, so putting the JAR in the classpath during compilation is only necessary to ensure that your code is using the classes from it correctly, but it does not actually embed them into your code as the linker would in C/C++. Thus, you need to set the classpath also when executing the code.
However, this:
$ javac Tool.java -classpath .:./extern/:
should not work either, since JARs need to be put into the classpath directly, not just the directory they live in:
$ javac Tool.java -classpath .:./extern/Hello.jar
Finally, you are placing your code in the default nameless package. This is OK for fooling around, but will cause problems in the long run (for one thing, you cannot import classes FROM the default package anywhere else).
When you run Java you must add the jar file too (adding the directory path only does not work).
See classpath information.
It should be something like this:
java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
You need to include the Hello.jar file in the classpath when you launch it too.
java -cp xxx.jar hello where xxx is the jar you want to have in your classpath, if you want multiple jars then separate them using ;
karl

Categories