Cannot use document javascript object when running from Java - java

I have the following javascript code that runs from java:
var doc = document.implementation.createDocument('http://www.w3.org/1999/xhtml', 'html', null);
doc.documentElement.innerHTML = xmlResponse;
And I get the following error:
Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "document" is not defined. (<Unknown source>#13)
at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3773)
at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3751)
at sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3836)
at sun.org.mozilla.javascript.internal.ScriptRuntime.name(ScriptRuntime.java:1762)
at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1790)
at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:854)
at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:164)
at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:429)
at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:116)
at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:109)
at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3163)
at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:175)
at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1159)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:210)
... 16 more
Do you have any clue of how to solve it?

According to the documentation:
The window object represents an open window in a browser.
Since you are not executing your script in a browser, the window object is not defined.
You can read the URL using the URL/URLConnecion classes and feed it to the ScriptEngine. There is a tutorial here.

Related

Getting version mismatch error while updating into DCTM using java

I am trying to update into DCTM through java code, below is the code snippet
IDfDocument communication = (IDfDocument) getDfSession().getObject(DfId.valueOf(communicationId));
communication.setString(ATTR_STATUS, status);
communication.save();
but I am getting the below error
Caused by: DfException:: THREAD: be.ing.ca.xpression.DCTM001P-1; MSG: [DM_OBJ_MGR_E_VERSION_MISMATCH]error: "save of object
090283e589bf689d of type xx_document failed because of version
mismatch: old version was 4"; ERRORCODE: 100; NEXT: null
I thinki am getting this error because there is another process which is trying to modify the object ,and when more than one process try to modify anyobject DCTM throws this exception,
But after lot of searching i dident found any solution which can solve this error
If anyone knows the solution please reply..
Link that i refer
http://www.javablog.fr/?s=version+mismatch
Try calling a fetch() on the object before doing updates.
communication.fetch()
There are some optional parameters AFAIK, but it's been a while since I've been fiddling with DCTM.
Best of luck!

Nashorn failed to call a static method of a 3rd party Java class, any solutions?

The code in Nashorn is to parse a very simple JSON string into a JSONObject (com.alibaba.fastjson.JSONObject), simply going as:
var jso = Java.type('com.alibaba.fastjson.JSONObject').parseObject('{"name": "nothing"}');
But when ran the Java project, I received below error message:
Exception in thread "main" javax.script.ScriptException: TypeError: Java.type("com.alibaba.fastjson.JSONObject").parseObject is not a function in <eval> at line number 1
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:454)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
...
I tried to print(Java.type('com.alibaba.fastjson.JSONObject')), and the class is obviously there with output [JavaClass com.alibaba.fastjson.JSONObject]
Anybody knows what's the root cause of this error, and how to fix it?

How to put javaScript code in Java program/application?

Trying to put in your application code of Ace Editor: http://ace.c9.io/#nav=embedding.
I found something like this: http://metoojava.wordpress.com/2010/06/20/execute-javascript-from-java/
and I put this code:
engine.eval(new java.io.FileReader("ace-builds/src-noconflict/ace.js"));
But I have build errors.
Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "window" is not defined. (<Unknown source>#1513) in <Unknown source> at line number 1513
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:224)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
at javaapplication9.JavaApplication9.main(JavaApplication9.java:28)
Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "window" is not defined. (<Unknown source>#1513)
at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3770)
at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3748)
at sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3833)
at sun.org.mozilla.javascript.internal.ScriptRuntime.nameOrFunction(ScriptRuntime.java:1826)
at sun.org.mozilla.javascript.internal.ScriptRuntime.name(ScriptRuntime.java:1765)
at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:1785)
at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:849)
at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:162)
at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:430)
at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:116)
at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:109)
at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:3160)
at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:173)
at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1169)
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:214)
... 2 more
Do any of you know how can I do this?
Thans for help and Happy Holidays!
Keep in mind that you are executing a Javascript file out of context.
This Javascript file is usually executed with the browser JS engine so if you are not in that context you could not access to some objects like navigator, window... etc.

Javascript native method throws unexpectedly exception

I am updating a vaadin project from vaadin 6 version to vaadin 7. It contains some native Javascript methods which work well in the old version. However some of them throw an exception when they should be called. I am not acquainted enough to Javascript programming and therefore I am not able to identify the source of the error. In particular the method is:
protected static native void addSthToImg()
/*-{
$wnd.$(document).ready(function() {
var = $wnd.$;
var('.settingswrapper > img').addClass("imgStartup");
if (var('.settingswrapper > img').hasClass("imgStartup")) {
console.log("imgStartup exist");
var('.settingswrapper > img')
.animate(null, 300, function() {
var('.settingswrapper > img').removeClass("imgStartup");
var('.settingswrapper > img').addClass("imagePopIn");
});
}
});
}-*/;
On the Console in Chrome I see the error stack trace:
Caused by: com.google.gwt.core.client.JavaScriptException: (TypeError) : Object [object global] has no method '$'
What could be a possible cause of the error and why is it working in the old version? Is a jar/library missing?
Seems like jQuery alias $ is not defined in the global scope (denoted by $wnd). Are you sure your page includes jQuery?
You should have something like <script src="/js/jquery.js" type="text/javascript"></script> (but pointing to the actual location of jquery.js in your project) in your HTML source. I don't know if Vaadin manages JavaScript libraries automatically, since I'm not familiar with the framework.

Creating index and adding mapping in Elasticsearch with java api gives missing analyzer errors

Code is in Scala. It is extremely similar to Java code.
Code that our map indexer uses to create index: https://gist.github.com/a16e5946b67c6d12b2b8
Utilities that the above code uses to create index and mapping: https://gist.github.com/4f88033204cd761abec0
Errors that java gives: https://gist.github.com/d6c835233e2b606a7074
Response of http://elasticsearch.domain/maps/_settings after running code and getting errors: https://gist.github.com/06ca7112ce1b01de3944
JSON FILES:
https://gist.github.com/bbab15d699137f04ad87
https://gist.github.com/73222e300be9fffd6380
Attached are the json files i'm loading in. I have confirmed that it is loading the right json files and properly outputting it as a string into .loadFromSource and .setSource.
Any ideas why it can't find the analyzers even though they are in _settings? If I run these json files via curl they work fine and properly setup the mapping.
The code I was using to create the index (found here: Define custom ElasticSearch Analyzer using Java API) was creating settings in the index like:
"index.settings.analysis.filter.my_snow.type: "stemmer","
It had settings in the setting path.
I changed my indexing code to the following to fix this:
def createIndex(client: Client, indexName: String, indexFile: String) {
//Create index
client.admin().indices().prepareCreate(indexName)
.setSource(Utils.loadFileAsString(indexFile))
.execute()
.actionGet()
}

Categories

Resources