
A description of running HelloWorld from the example offered by Oracle in “Getting Started with JavaFX”, but without any idea, that is, without IntelliJ IDEA and generally any IDE. Implemented on Windows PC. Opus belongs to the class of "Kettle - teapot." Suddenly, it turned out that there is not enough information in the tutorial for compiling and running the simplest application from the command line that contains the code for this application.
To an experienced person such notes can seem funny. However, for those who have just approached a field dotted with a rake, such a guide can be useful.
')
Ask - why, exactly? For learning, like all HelloWorlds. I do not want to detract from the IDE values and declare: “Only a notebook! Only hardcore! ”But the command line allows, if not to understand all the mechanics, then at least look under the hood. Especially when there is already some programming experience
for food . Suddenly, there was a desire to expand horizons and master, well, let's say, Java. At least try.
There is a completely wonderful post
Working with Java in the command line from
Qwertovsky . Still, for further immersion is not enough bare console, I want windows. Preferably in the form of a desktop application. I read what are the options for creating a GUI for Java-programs, and what people think about them, while stopped at JavaFX. It turned out that quite a little was written about Russian in various kinds of Internet about JFX, and an essential part about the first version. So you have to dig on the Oracle site.
jfxpub-overview tells us that since JavaFX applications are written in the Java language, I can use my favorite editor or some IDE (NetBeans, Eclipse or IntelliJ IDEA) to create them. It is proposed to take a look at
www.oracle.com and download Oracle JDK 7 with JavaFX 2.2.n support, and then use the
jfxpub-get_started tutorial to create a simple application that demonstrates working with layers, style sheets and visual effects. They also suggest using JavaFX Scene Builder to develop a user interface without coding, but this is something later.
JDK installed and (just in case) JRE. I did not register anything in the PATH system variable, and I did not manually register.
Take this example as it is.
HelloWorld.javapackage helloworld; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class HelloWorld extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } }
The authors of the manual suggest using NetBeans IDE 7.3. We will manage. Make a folder for the project, let's say D: \ GetStart \, in it we create a folder for the src sources and a folder for the out compilation results. Since the trial class belongs to the helloworld package, you need to make a folder of the same name in src and put the HelloWorld.java file there.
To compile this, we will make the file compile.cmd in the root of the project. So it is more convenient to make changes to the compilation command - you do not have to rely on the memory of the command line interpreter, and in general, you can do without cmd.exe, use your favorite file manager, and at least a guide.
jfxpub-overview explained that JavaFX is fully embedded in the Java SE 7 JRE and JDK. On the
downloads page, those wishing to download JavaFX for Java SE 7 simply offer to download Java SE 7. There are no javafxc and javafx files among the JDK files. Apparently, you can compile a JFX application just like any other java file. Therefore, in the command file, we write two lines:
@"C:\Program Files\Java\jdk1.7.0_40\bin\javac" -d out src\helloworld\HelloWorld.java @pause
The second line gives the opportunity to review the error messages. If in the cmd.exe window that appears, there will be a single message “To continue, press any key ...”, then the previous command was executed without errors.
However, the launch will produce a long sheet, which, probably, the whole does not even fit into the output window. 18 errors, something not found, something does not exist. To view all messages completely, the first line of the CMD file can be supplemented by redirecting the error output stream to the file “2> result.txt”.
Errors src\helloworld\HelloWorld.java:3: error: package javafx.application does not exist import javafx.application.Application; ^ src\helloworld\HelloWorld.java:4: error: package javafx.event does not exist import javafx.event.ActionEvent; ^ ... symbol: class Scene location: class HelloWorld src\helloworld\HelloWorld.java:15: error: method does not override or implement a method from a supertype @Override ^ 18 errors
The compiler cannot find the JFX packages. It is necessary to prompt him, and for this you need to find out yourself. Maybe I didn’t rummage very carefully, but I didn’t find it on the Oracle website, or rather I found it only on the
forum . The required file is jfxrt.jar (logically -
J ava
F X R un
T ime), and it lies in the JRE, that is, I have, for example, in C: \ Program Files \ Java \ jre7 \ lib and C: \ Program Files \ Java \ jdk1.7.0_40 \ jre \ lib. Add the -classpath option:
@"C:\Program Files\Java\jdk1.7.0_40\bin\javac" -d out -classpath "C:\Program Files\Java\jre7\lib\jfxrt.jar" src\helloworld\HelloWorld.java @pause
Now compiled. The folder ./out/helloworld was created, and in it the HelloWorld.class file. If you indulge in the .java file to rename the package to the HelloWorld package, then the folder will be created in the corresponding register - ./out/HelloWorld, only the application will not start.
I would like to run. In the above-mentioned topic on the Oracle forum, a jar-packaged version is proposed, but we will first try to launch it like this. Let's make another run.cmd command file with the JFX library specified in advance:
@"C:\Program Files\Java\jdk1.7.0_40\bin\java" -classpath "C:\Program Files\Java\jre7\lib\jfxrt.jar" helloworld.HelloWorld @pause
When starting, we get an error:
Error: Could not find or load main class helloworld.HelloWorld
Well, it means that the classpath is still too small, we must indicate the residence of our compiled class:
@"C:\Program Files\Java\jdk1.7.0_40\bin\java" -classpath "C:\Program Files\Java\jre7\lib\jfxrt.jar;.\out" helloworld.HelloWorld @pause
Running this command will finally bring up a window with a button from the get_started illustration. At the same time, the cmd.exe console will also be visible. Clicking on the button in the window will result in the message “Hello World!” In the console.
I would like to pack in the jar. Let's use the javafxpackager utility. Launched in the console without parameters, it gives quite detailed and understandable information on its options. You can make the file jar.cmd:
@"C:\Program Files\Java\jdk1.7.0_40\bin\javafxpackager" -createjar -appclass helloworld.HelloWorld -srcdir .\out -outfile HelloWorld -v @pause
From the explorer, the resulting jar is triggered by double-clicking on me. However, the file manager enters this file, as in the archive (which the file itself is). In addition, in Explorer file is run without a console. Therefore, for convenience, I added run_jar.cmd:
@"C:\Program Files\Java\jre7\bin\java.exe" -jar HelloWorld.jar @pause
Works!
Further, the manual proposes making a login and password entry window to learn the basics of placing elements, then a stylish beauty is made from this window using cascading style sheets (CSS), then the same form is drawn, but using FXML. In all these actions scattered a little rake. Nothing particularly difficult - just non-obviousness, which the manual does not mention, and the program as a result does not compile or run.