I have written a little Clock Widget extending the Text class of JavaFX. To update the Time I am using a Task that basically sets the text to the current System Time. When I run this application in Eclipse it sometimes throws a NullpointerException in the line I call stage.show().
This is what my widgets sourcecode looks like:
package clock;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javafx.concurrent.Task;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class CurrentClockText extends Text {
private final DateTimeFormatter formatter;
public static final int HOUR = 1;
public static final int HOUR_MINUTE = 2;
public static final int HOUR_MINUTE_SECOND = 3;
public static final int HOUR_MINUTE_SECOND_MILLISECOND = 4;
private final long updateInterval;
private final Task<Void> updater = new Task<Void>() {
#Override
protected Void call() throws Exception {
while (true) {
LocalDateTime now = LocalDateTime.now();
String nowTextual = formatter.format(now);
setText(nowTextual);
try {
Thread.sleep(updateInterval);
} catch (InterruptedException ignore) {
}
}
}
};
public CurrentClockText() {
this(HOUR_MINUTE);
}
public CurrentClockText(final int detailLevel) {
String timeFormat = "";
switch (detailLevel) {
case HOUR:
timeFormat = "HH";
updateInterval = 60000;
break;
case HOUR_MINUTE:
timeFormat = "HH:mm";
updateInterval = 15000;
break;
case HOUR_MINUTE_SECOND:
timeFormat = "HH:mm:ss";
updateInterval = 500;
break;
case HOUR_MINUTE_SECOND_MILLISECOND:
updateInterval = 1;
timeFormat = "HH:mm:ss.S";
break;
default:
throw new IllegalArgumentException(
"Unknown detail level for Clock: " + detailLevel);
}
setFont(new Font("Verdana", 28));
formatter = DateTimeFormatter.ofPattern(timeFormat);
Thread updaterThread = new Thread(updater, "CurrentClockText.updaterThread");
updaterThread.setDaemon(true);
updaterThread.start();
}
}
This is the main class:
package clock;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Boot extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
CurrentClockText clock = new CurrentClockText(4);
pane.setCenter(clock);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Boot.launch(args);
}
}
And this is the stacktrace:
Exception in Application start method
java.lang.reflect.InvocationTargetException
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
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 sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/14845382.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at com.sun.javafx.text.PrismTextLayout.addTextRun(Unknown Source)
at com.sun.javafx.text.GlyphLayout.addTextRun(Unknown Source)
at com.sun.javafx.text.GlyphLayout.breakRuns(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.buildRuns(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.layout(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.ensureLayout(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.getBounds(Unknown Source)
at javafx.scene.text.Text.getLogicalBounds(Unknown Source)
at javafx.scene.text.Text.impl_computeLayoutBounds(Unknown Source)
at javafx.scene.Node$12.computeBounds(Unknown Source)
at javafx.scene.Node$LazyBoundsProperty.get(Unknown Source)
at javafx.scene.Node$LazyBoundsProperty.get(Unknown Source)
at javafx.scene.Node.getLayoutBounds(Unknown Source)
at javafx.scene.Node.prefWidth(Unknown Source)
at javafx.scene.Node.minWidth(Unknown Source)
at javafx.scene.layout.Region.computeChildPrefAreaWidth(Unknown Source)
at javafx.scene.layout.BorderPane.getAreaWidth(Unknown Source)
at javafx.scene.layout.BorderPane.computePrefWidth(Unknown Source)
at javafx.scene.Parent.prefWidth(Unknown Source)
at javafx.scene.layout.Region.prefWidth(Unknown Source)
at javafx.scene.Scene.getPreferredWidth(Unknown Source)
at javafx.scene.Scene.resizeRootToPreferredSize(Unknown Source)
at javafx.scene.Scene.preferredSize(Unknown Source)
at javafx.scene.Scene.impl_preferredSize(Unknown Source)
at javafx.stage.Window$9.invalidated(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
at javafx.stage.Window.setShowing(Unknown Source)
at javafx.stage.Window.show(Unknown Source)
at javafx.stage.Stage.show(Unknown Source)
at clock.Boot.start(Boot.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/19600960.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/18503843.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/27167109.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2180324.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/3326003.run(Unknown Source)
... 1 more
Exception running application clock.Boot
As I said this Exception only occurs in some runs and not always. It happens mostly when I change the formatting pattern of detailLevel = 4 but remains for another run after changing it back again. I am guessing this might actually have something to do with Eclipse but I can't debug the code either because the Exception gets thrown in the stage.show call which I absolutely do not understand. What's the cause of this random Exception and how can I fix it?
In JavaFX, as in most other GUI toolkits, there is one specific thread which handles all UI related operations. An application must not update the UI outside of this thread. If the UI needs to be updated from another thread, there are usually APIs available which ensure that code is executed in the context of the UI thread. In case of JavaFX, see the Concurrency in JavaFX Tutorial for more information.
In your case, the simplest solution would be to make sure that your setText() call is executed on the JavaFX application thread, not on the thread associated with your Task:
...
Platform.runLater(() -> setText(nowTextual));
...
There are also other APIs available in JavaFX to do animations, which can be used to call a handler method at specific time intervals - that would remove the Thread.sleep() call from your loop. See Timeline for more information.
Related
I'm looking for a way around the problem with showAndWait call.
I've thought onFinishedProperty is fired when the animation process is over, still im unable to call showAndWait because of error saying that im trying to call it during animation process. Is there anyway to call my dialog right after finish of animation?
TextInputDialog dialog = new TextInputDialog("Litera");
dialog.setTitle("");
dialog.setHeaderText("");
dialog.setContentText("Wybierz literę:");
for(int i = 0 ; i<pieChartData.size();i++)
System.out.println(pieChartData.get(i));
RotateTransition rotateTransition = new RotateTransition();
rotateTransition.setDuration(Duration.millis(5000));
rotateTransition.setNode(fortuna);
rotateTransition.setCycleCount(1);
rotate.setOnAction(
(e)->{
fortuna.setRotate(0);
rotate.setDisable(true);
rotateTransition.setAutoReverse(false);
rotateTransition.setByAngle((int)(Math.random()*10800+360));
rotateTransition.play();
rotateTransition.onFinishedProperty().set(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println(fortuna.getRotate());
Optional<String> result = dialog.showAndWait();
rotate.setDisable(false);
}
}
);
});
Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: showAndWait is not allowed during animation or layout processing
at javafx.controls/javafx.scene.control.Dialog.showAndWait(Unknown Source)
at Main$1.handle(Main.java:78)
at Main$1.handle(Main.java:1)
at javafx.graphics/javafx.animation.Animation.finished(Unknown Source)
at javafx.graphics/javafx.animation.AnimationAccessorImpl.finished(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.shared.SingleLoopClipEnvelope.timePulse(Unknown Source)
at javafx.graphics/javafx.animation.Animation.doTimePulse(Unknown Source)
at javafx.graphics/javafx.animation.Animation$1.lambda$timePulse$0(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/javafx.animation.Animation$1.timePulse(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
I'm currently developing an Extension for OpenOffice. I'm using Java 1.6 and the OpenOffice SDK 4.1.2.
If I try to create a javax.swing.JTextPane, I get a NullpointerException in the Constructor of JTextPane.
public class Dialog extends javax.JFrame {
private final JTextPane jTextPane;
private final JTable jTable;
public Dialog() {
jTable = new JTable();
jTextPane = new JTextPane();
}
}
The Dialog is initialized in another Thread:
public class DialogManager {
private static JournalDialog journalDialog;
public void showDialog() {
Thread startThread = new Thread(new Runnable() {
#Override
public void run() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
dialog = new Dialog();
...
}
}
}
}
}}
The creation of JTable works fine, but in the next line I get a Nullpointerexception
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
at javax.swing.JEditorPane.loadDefaultKitsIfNecessary(Unknown Source)
at javax.swing.JEditorPane.getKitTypeRegistry(Unknown Source)
at javax.swing.JEditorPane.getEditorKitClassNameForContentType(Unknown Source)
at javax.swing.JTextPane.<init>(Unknown Source)
at .gui.Dialog.<init>(Dialog.java:159)
at .gui.DialogManager$6$1.run(DialogManager.java:334)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I hope someone has an idea what causes this exception. I tried to run the extension in Java 1.7 and 1.8, but there is the same issue.
Best regards
Update 16.08.2016:
If I add a JTexPane over the Netbeans Palette into the Designer, it works. Only the initialisation in the constructor fails.
It may not be possible to use Swing for this task without crashing. Instead, use the com.sun.star.awt module. Complete examples are at http://api.libreoffice.org/examples/DevelopersGuide/examples.html#GraphicalUserInterfaces.
For more information, see Creating Dialogs at Runtime.
One more link: This example does use Swing. Try it to see if the same problem occurs.
My dirty solution is to initialize the JTextPane twice within a try-catch. Because on the second call i don't get an exception.
I'm getting NullPointerException when using org.controlsfx.control.RangeSlider. Here is the simple code example:
package experimental_main;
import org.controlsfx.control.RangeSlider;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Experimental_start extends Application
{
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception
{
Button button = new Button("Click me");
RangeSlider rangeSlider = new RangeSlider(0, 10, 2, 4);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(rangeSlider);
borderPane.setTop(button);
Scene scene = new Scene(borderPane, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
The exception occurs when pressing Tab while right thumb of RangeSlider is focused or pressing Shift+Tab while its left thumb is focused. These key pressings cause to lose focus from RangeSlider as expected, but this action also causes NullPointerException. Here is exception's stack trace:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.traversal.TraversalEngine.select(Unknown Source)
at impl.org.controlsfx.skin.RangeSliderSkin$5.handle(RangeSliderSkin.java:150)
at impl.org.controlsfx.skin.RangeSliderSkin$5.handle(RangeSliderSkin.java:132)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$KeyHandler.process(Unknown Source)
at javafx.scene.Scene$KeyHandler.access$1800(Unknown Source)
at javafx.scene.Scene.impl_processKeyEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$353(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(Unknown Source)
at com.sun.glass.ui.View.handleKeyEvent(Unknown Source)
at com.sun.glass.ui.View.notifyKey(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
There is no exception when pressing Tab (or Shift+Tab) with a view to make RangeSlider's thumb focused.
Am I doing something wrong or it's a bug? Is there alternative to RangeSlider class realizations?
This listener of the RangeSliderSkin seems to cause the NPE:
EventHandler<KeyEvent> keyEventHandler = new EventHandler<KeyEvent>() {
#Override public void handle(KeyEvent event) {
if (KeyCode.TAB.equals(event.getCode())) {
if (lowThumb.isFocused()) {
if (event.isShiftDown()) {
lowThumb.setFocus(false);
new ParentTraversalEngine(rangeSlider).select(rangeSlider, Direction.PREVIOUS);
} else {
lowThumb.setFocus(false);
highThumb.setFocus(true);
}
event.consume();
} else if (highThumb.isFocused()) {
if(event.isShiftDown()) {
highThumb.setFocus(false);
lowThumb.setFocus(true);
} else {
highThumb.setFocus(false);
new ParentTraversalEngine(rangeSlider).select(rangeSlider, Direction.NEXT);
}
event.consume();
}
}
}
};
To be more specific, the line new ParentTraversalEngine(rangeSlider).select(rangeSlider, Direction.NEXT);
ParentTraversalEngine is initalized without an Algorithm argument, so calling select(rangeSlider, Direction.NEXT) is causing a NPE, as it is supposed to:
Throws:
java.lang.NullPointerException - if there is no algorithm
javadoc
The bug is fixed beginning from version 8.40.13.
I'm trying to integrate Java Skype API in my program. I found an example code in the API documentation that was using Java listeners to get the current call status. I implemented the code in my program and I need to call a function (addLog function) every time the Skype call status changes, but when I call such function, the console is showing me a "Exception in thread "AsyncSkypeMessageSender-18" java.lang.IllegalStateException".
When I use the command System.out.ln inside the listener, it is working how it is supposed to, without any errors, but every time I call a function inside the listener, it gives me an error.
Do you guys know something to solve my problem, I thought it may be a thread related issue, but I'm not familiar with Java programming.
The code is:
public void addLog(String log){
Date date = new Date();
SimpleDateFormat datetime = new SimpleDateFormat("dd/MM hh:mm:ss");
String datetimeString = datetime.format(date);
logArea.appendText(datetimeString + " > " + log);
}
public void makeCall() throws SkypeException {
Skype.call("echo123");
Skype.setDaemon(false);
//Skype.setDebug(true);
System.out.println(" --------------------------------");
System.out.println(" ------ Fazendo ligação... ------");
System.out.println(" --------------------------------");
SkypeVoicer skypeVoicer = new SkypeVoicer();
skypeVoicer.addLog("Iniciou ligação\n");
Skype.addCallListener(new CallAdapter() {
public void callMaked(Call makedCall){
makedCall.addCallStatusChangedListener(new CallStatusChangedListener(){
#Override
public void statusChanged(Status status) throws SkypeException {
if(status == Status.FINISHED){
System.out.println("Finalizou, chamada de " + makedCall.getDuration() + " segundos...");
skypeVoicer.addLog("\nFinalizou em "+makedCall.getDuration()+" segundos");
makedCall.removeCallStatusChangedListener(this);
}
else if(status == Status.CANCELLED){
System.out.println("Cancelado");
}
else if(status == Status.ROUTING){
System.out.println("Redirecionando...");
}
else if(status == Status.RINGING){
System.out.println("Tocando...");
}
else if(status == Status.INPROGRESS){
System.out.println("Em Progresso...");
skypeVoicer.addLog("Em Progresso...");
}
}
});
}
});
}
The full stack trace is this one bellow:
Exception in thread "AsyncSkypeMessageSender-18"
java.lang.IllegalStateException: Not on FX application thread;
currentThread = AsyncSkypeMessageSender-18 at
com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source) at
com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown
Source) at javafx.scene.Scene.addToDirtyList(Unknown Source) at
javafx.scene.Node.addToSceneDirtyList(Unknown Source) at
javafx.scene.Node.impl_markDirty(Unknown Source) at
javafx.scene.shape.Shape.impl_markDirty(Unknown Source) at
javafx.scene.text.Text.geomChanged(Unknown Source) at
javafx.scene.text.Text.impl_geomChanged(Unknown Source) at
javafx.scene.text.Text.needsTextLayout(Unknown Source) at
javafx.scene.text.Text.needsFullTextLayout(Unknown Source) at
javafx.scene.text.Text.access$200(Unknown Source) at
javafx.scene.text.Text$2.invalidated(Unknown Source) at
javafx.beans.property.StringPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.StringPropertyBase.set(Unknown Source) at
javafx.beans.property.StringPropertyBase.set(Unknown Source) at
javafx.scene.text.Text.setText(Unknown Source) at
com.sun.javafx.scene.control.skin.TextAreaSkin.lambda$new$231(Unknown
Source) at
com.sun.javafx.scene.control.skin.TextAreaSkin$$Lambda$172/1330104206.invalidated(Unknown
Source) at
com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(Unknown
Source) at
com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown
Source) at
javafx.scene.control.TextInputControl$TextProperty.fireValueChangedEvent(Unknown
Source) at
javafx.scene.control.TextInputControl$TextProperty.markInvalid(Unknown
Source) at
javafx.scene.control.TextInputControl$TextProperty.invalidate(Unknown
Source) at
javafx.scene.control.TextInputControl$TextProperty.access$1300(Unknown
Source) at
javafx.scene.control.TextInputControl.lambda$new$163(Unknown Source)
at
javafx.scene.control.TextInputControl$$Lambda$77/323047656.invalidated(Unknown
Source) at
com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(Unknown
Source) at
com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown
Source) at
javafx.scene.control.TextArea$TextAreaContent.insert(Unknown Source)
at javafx.scene.control.TextInputControl.replaceText(Unknown Source)
at javafx.scene.control.TextInputControl.insertText(Unknown Source)
at javafx.scene.control.TextInputControl.appendText(Unknown Source)
at SkypeVoicer.addLog(SkypeVoicer.java:186) at
SkypeVoicer$2$1.statusChanged(SkypeVoicer.java:157) at
com.skype.Call.fireStatusChanged(Call.java:339) at
com.skype.ConnectorListenerImpl.messageReceived(ConnectorListenerImpl.java:61)
at
com.skype.connector.Connector.fireMessageEvent(Connector.java:1160)
at com.skype.connector.Connector.access$500(Connector.java:37) at
com.skype.connector.Connector$13.run(Connector.java:1139) at
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at
java.lang.Thread.run(Unknown Source)
I'm using Rhino 1.7 R4 with envjs 1.2 on Mac OSX with JDK 1.6.0_33
If I run:
java -jar rhino-1.7R4.jar -opt -1
And then:
load('env.rhino-1.2.js')
Then the script is loaded successfully.
When I load the same JS script from Java via RhinoTest.java:
import org.apache.commons.io.IOUtils;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextAction;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.tools.shell.Main;
import org.mozilla.javascript.tools.shell.ShellContextFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class RhinoTest {
static final Logger LOGGER = LoggerFactory.getLogger(RhinoTest.class);
static private Scriptable SCRIPTABLE;
static private List<Script> scripts;
public static void main(String[] args) {
ShellContextFactory cxFactory = Main.shellContextFactory;
cxFactory.call(new ContextAction() {
#Override
public Object run(Context cx) {
final Initiator initiator = new Initiator(cx).init();
LOGGER.trace("Run script");
try {
return initiator.execute();
} finally {
}
}
class Initiator {
Initiator(Context context) {
this.cx = context;
}
Initiator init() {
if (SCRIPTABLE == null)
createScriptable();
initContext();
return this;
}
Object execute() {
return null;
}
void createScriptable() {
LOGGER.trace("init standard objects");
SCRIPTABLE = cx.initStandardObjects();
LOGGER.trace("set optimization level to -1");
cx.setOptimizationLevel(-1);// bypass the 64k limit // interpretive mode
attachJs("env.rhino-1.2.js");
LOGGER.trace("set optimization level to 9");
cx.setOptimizationLevel(9);
}
#SuppressWarnings("deprecation")
private void attachJs(String jsFileName) {
InputStream in = null;
InputStreamReader reader = null;
if (LOGGER.isDebugEnabled()) LOGGER.debug("loading " + jsFileName);
try {
in = RhinoTest.class.getResourceAsStream(jsFileName);
if (in == null)
throw new RuntimeException("cannot find js file : " + jsFileName);
reader = new InputStreamReader(in);
if (scripts == null)
scripts = new ArrayList<Script>();
scripts.add(cx.compileReader(SCRIPTABLE, reader, jsFileName, 1, null));
if (LOGGER.isDebugEnabled()) LOGGER.debug("loaded " + jsFileName);
} catch (IOException e) {
throw new RuntimeException("cannot load js file : " + jsFileName, e);
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(reader);
}
}
void initContext() {
LOGGER.trace("set optimization level to -1");
cx.setOptimizationLevel(-1);// bypass the 64k limit // interpretive mode
for (Script s : scripts) {
s.exec(cx, SCRIPTABLE);
}
}
private final Context cx;
}
});
}
}
I get:
Exception in thread "main" org.mozilla.javascript.EvaluatorException: missing : after property id (env.rhino-1.2.js#2121)
at org.mozilla.javascript.DefaultErrorReporter.runtimeError(Unknown Source)
at org.mozilla.javascript.DefaultErrorReporter.error(Unknown Source)
at org.mozilla.javascript.Parser.addError(Unknown Source)
at org.mozilla.javascript.Parser.reportError(Unknown Source)
at org.mozilla.javascript.Parser.mustMatchToken(Unknown Source)
at org.mozilla.javascript.Parser.primaryExpr(Unknown Source)
at org.mozilla.javascript.Parser.memberExpr(Unknown Source)
at org.mozilla.javascript.Parser.unaryExpr(Unknown Source)
at org.mozilla.javascript.Parser.mulExpr(Unknown Source)
at org.mozilla.javascript.Parser.addExpr(Unknown Source)
at org.mozilla.javascript.Parser.shiftExpr(Unknown Source)
at org.mozilla.javascript.Parser.relExpr(Unknown Source)
at org.mozilla.javascript.Parser.eqExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitAndExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitXorExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitOrExpr(Unknown Source)
at org.mozilla.javascript.Parser.andExpr(Unknown Source)
at org.mozilla.javascript.Parser.orExpr(Unknown Source)
at org.mozilla.javascript.Parser.condExpr(Unknown Source)
at org.mozilla.javascript.Parser.assignExpr(Unknown Source)
at org.mozilla.javascript.Parser.argumentList(Unknown Source)
at org.mozilla.javascript.Parser.memberExprTail(Unknown Source)
at org.mozilla.javascript.Parser.memberExpr(Unknown Source)
at org.mozilla.javascript.Parser.unaryExpr(Unknown Source)
at org.mozilla.javascript.Parser.mulExpr(Unknown Source)
at org.mozilla.javascript.Parser.addExpr(Unknown Source)
at org.mozilla.javascript.Parser.shiftExpr(Unknown Source)
at org.mozilla.javascript.Parser.relExpr(Unknown Source)
at org.mozilla.javascript.Parser.eqExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitAndExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitXorExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitOrExpr(Unknown Source)
at org.mozilla.javascript.Parser.andExpr(Unknown Source)
at org.mozilla.javascript.Parser.orExpr(Unknown Source)
at org.mozilla.javascript.Parser.condExpr(Unknown Source)
at org.mozilla.javascript.Parser.assignExpr(Unknown Source)
at org.mozilla.javascript.Parser.expr(Unknown Source)
at org.mozilla.javascript.Parser.statementHelper(Unknown Source)
at org.mozilla.javascript.Parser.statement(Unknown Source)
at org.mozilla.javascript.Parser.parseFunctionBody(Unknown Source)
at org.mozilla.javascript.Parser.function(Unknown Source)
at org.mozilla.javascript.Parser.primaryExpr(Unknown Source)
at org.mozilla.javascript.Parser.memberExpr(Unknown Source)
at org.mozilla.javascript.Parser.unaryExpr(Unknown Source)
at org.mozilla.javascript.Parser.mulExpr(Unknown Source)
at org.mozilla.javascript.Parser.addExpr(Unknown Source)
at org.mozilla.javascript.Parser.shiftExpr(Unknown Source)
at org.mozilla.javascript.Parser.relExpr(Unknown Source)
at org.mozilla.javascript.Parser.eqExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitAndExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitXorExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitOrExpr(Unknown Source)
at org.mozilla.javascript.Parser.andExpr(Unknown Source)
at org.mozilla.javascript.Parser.orExpr(Unknown Source)
at org.mozilla.javascript.Parser.condExpr(Unknown Source)
at org.mozilla.javascript.Parser.assignExpr(Unknown Source)
at org.mozilla.javascript.Parser.expr(Unknown Source)
at org.mozilla.javascript.Parser.primaryExpr(Unknown Source)
at org.mozilla.javascript.Parser.memberExpr(Unknown Source)
at org.mozilla.javascript.Parser.unaryExpr(Unknown Source)
at org.mozilla.javascript.Parser.mulExpr(Unknown Source)
at org.mozilla.javascript.Parser.addExpr(Unknown Source)
at org.mozilla.javascript.Parser.shiftExpr(Unknown Source)
at org.mozilla.javascript.Parser.relExpr(Unknown Source)
at org.mozilla.javascript.Parser.eqExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitAndExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitXorExpr(Unknown Source)
at org.mozilla.javascript.Parser.bitOrExpr(Unknown Source)
at org.mozilla.javascript.Parser.andExpr(Unknown Source)
at org.mozilla.javascript.Parser.orExpr(Unknown Source)
at org.mozilla.javascript.Parser.condExpr(Unknown Source)
at org.mozilla.javascript.Parser.assignExpr(Unknown Source)
at org.mozilla.javascript.Parser.expr(Unknown Source)
at org.mozilla.javascript.Parser.statementHelper(Unknown Source)
at org.mozilla.javascript.Parser.statement(Unknown Source)
at org.mozilla.javascript.Parser.parse(Unknown Source)
at org.mozilla.javascript.Parser.parse(Unknown Source)
at org.mozilla.javascript.Context.compileImpl(Unknown Source)
at org.mozilla.javascript.Context.compileReader(Unknown Source)
at org.mozilla.javascript.Context.compileReader(Unknown Source)
at RhinoTest$1$Initiator.attachJs(RhinoTest.java:85)
at RhinoTest$1$Initiator.createScriptable(RhinoTest.java:63)
at RhinoTest$1$Initiator.init(RhinoTest.java:44)
at RhinoTest$1.run(RhinoTest.java:28)
at org.mozilla.javascript.Context.call(Unknown Source)
at org.mozilla.javascript.ContextFactory.call(Unknown Source)
at RhinoTest.main(RhinoTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Please help
Wladimir,
You were right it was a version thing.
It turns out that the batik libs I imported included another version of rhino.
By excluding batik-js from my project for each batik dependency this issue went away.