How to tell JavaFX WebView to ignore 'use strict' directive? - java

I am trying to integrate the mozilla viewer in a JavaFx WebView by using the following code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class TestStrict extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
WebView webView = new WebView();
String url = TestStrict.class.getClassLoader().getResource("pdfjs-1.1.366-dist/web/viewer.html").toExternalForm();
webView.getEngine().load(url);
Scene scene = new Scene(webView);
primaryStage.setScene(scene);
primaryStage.setWidth(800);
primaryStage.setHeight(600);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The pdfjs-1.1.366-dist folder is downloaded from pdfjs GitHub
I also changed the viewer.html just to add firebug-lite inside:
<script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
Now when I run the application I get the viewer frame but without the default pdf loaded and the following error is inside firebug console:
"TypeError: undefined is not an object (evaluating 'globalScope.PDFJS') (pdf.worker.js,103)"
I removed all the 'use strict' directives in the javascript files and everything is working fine.
I don't know if this is a bug in JavaFX or internal WebKit but it happens with version 1.8.0_60.
So is there any way that I can disable strict mode since there will be other web pages that will be loaded where I can't control the scripts and remove 'use strict' directives?

Related

How to solve JavaFX runtime components are missing on VS code IDE

I'm using JavaFx on visual studio code IDE. I always get this error:
JavaFX runtime components are missing and are required to run this application
I've already added the VM args and the javafx libraries.
Also some basic FX codes are compiled with ease, but once I use the FXMLLoader class, I get the aforementioned error.
package app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application
{
#Override
public void start(Stage stage)
{
try
{
Parent root = FXMLLoader.load(getClass().getResource("/interfaces/SignIn.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
}
In launch.json, adding a new entry for the vmArgs including the --module-path with the local path to the JavaFX SDK and --add-modules with the required JavaFX modules:
"vmArgs": "--module-path /path/to/javafx/lib --add-modules javafx.controls,javafx.fxml"

How to Fix NoClassDefFoundError in JavaFX?

I'm trying to build a small user interface using JavaFX, But I'm getting an error like this:
Error: Could not find or load main class myApp Caused by:
java.lang.NoClassDefFoundError: javafx/application/Application
This is my code:
and im using jdk 12.0.2
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class myApp extends Application{
public static void main(String[] args) {
Phonebook mybook = new Phonebook();
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Group group = new Group();
Scene scene = new Scene(group, 600, 300);
scene.setFill(Color.GRAY);
primaryStage.setTitle("Phone Book");
primaryStage.setScene(scene);
primaryStage.show();
}
This is the Libraries and jdk I'm using:
Image1
I think JavaFX is not a part of the JDK > 9 any more. (Your version is 12.x.x)
Probably this could help you:
https://openjfx.io/openjfx-docs/#install-javafx
(Assuming you are using Maven) If this does not help, try to clean and build your Application.
Sometimes Maven does not recognize newly added dependencies.

javafx import cannot be resolved in eclipse

I am starting an assignment and just installed javafx from the Eclipse marketplace, after I installed and created a javafx project with fxml however in main the imports are showing errors saying "the import javafx cannot be resolved". I am new to Java and Eclipse and have tried to search for similar questions but they all seem to be different cases.
I would appreciate any help thank you.
Below is the code, and all the imports are showing errors:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Student.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
I have figured that it is the library JavaFx SDK which is not able to be added to the project, is anyone good with Eclipse can help solve this problem? I tried configure build path and removing the JavaFx SDK and readding, still does not work.

How to add a style.css to my application through the scene builder in JavaFX?

I am trying to add style.css to my java application in intellij. So I have added a new directory named resources and put the style.css inside that folder. Inside my scene builder I have specified in the GridPane that IO want the file "style.css" in my resources to control the view. However, whenever I run the code I get this error.
Error:
Oct 29, 2018 3:56:58 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not find stylesheet: file:/C:/Users/Troy22/Desktop/Java/EMCGrabExtension/out/production/resources/style.css
I tried different ways, but non have seemed to help. Thanks again for your help
Code:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root,770,450);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
scene.getStylesheets().add("style.css");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Try Adding it like this in your Main:
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
You can try adding it through scenebuilder (On the top hierarchy node, so if your root node is a BorderPane than add it on this node):
However I would recommend adding it like the comment before mine with:
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
Don't forget the .toExternalForm() after the getResource() method.
And be sure your resource folder is in the classpath so Java will find it.

Applet's Packages attribute is null JRE7u21

With a JavaFX applet :
The javascript object I get with document.getElementById("APPLET_ID") has no Packages attribute under Windows.
I run my tests on Windows XP with IE8, FF and Chrome up to date, but it's the same problem under windows 7.
Under Ubuntu with JRE 7u7 x64, no such problem.
Here is my test code :
package test;
import netscape.javascript.JSObject;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
private JSObject js;
TextField tf;
#Override
public void start(Stage primaryStage) {
js = this.getHostServices().getWebContext();
HBox hb = new HBox();
Scene s = new Scene(hb, 400, 400);
tf = new TextField("MAIN");
primaryStage.setScene(s);
hb.getChildren().add(tf);
primaryStage.show();
runTest();
}
public static void main(String[] args) {
launch(args);
}
void runTest(){
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(
new Runnable() {
#Override
public void run() {
tf.setText("" + js.eval("document.getElementById('applet_id').Packages == null"));
}
}
);
}
}).start();
}
}
Displays "false" under Ubuntu JRE7u7x64 and "true" under Windows and Ubuntu JRE7u21 with all browsers.
As the JavaFX2 deployment doc page tells I'm doing it the right way, it looks like a JRE bug. What do you think about it ?
http://docs.oracle.com/javafx/2/deployment/javafx_javascript.htm .
Filed on Jira : https://javafx-jira.kenai.com/browse/RT-30732
Looks it's an expected change since 7u21 : http://www.oracle.com/technetwork/java/javase/7u21-relnotes-1932873.html
This Packages attribute no more works.
So if you need callbacks from your JS to your applet, you must access directly it's methods.
And use the Trusted-Library manifest attribute to avoid warnings.
But you can't do what you want when using Trusted-Library. For example, if you use Axis2 webservices.
So you'll have to do it with Trusted-Only manifest attribute. But this forbids you to call methods from JS.
Simple workaround :
Have a thread periodically checking a JS callback queue and treat them. It's painful, horrible POJO but it works.
So to Oracle :
THANKS
for updating online doc
for announcing major changes made in minor releases
for making web integration so easy.
for adding other security checks in u25 that result in 20 seconds gray screen latency
Do you know Adobe ? Heard they have pretty good stuff... I'm pissed.

Categories