I'm trying to check velocity scripting engine 2.0 which Provide JSR 223 implementation and support of Compilable
the Compilable interface has been implemented in the process.
I use jars: velocity-engine-scripting-2.0.jar, velocity-1.7.jar, commons-collections-3.2.2.jar
from previous answer I use the following code
//class org.apache.velocity.script.VelocityScriptEngine
final ScriptEngine engine = engineFactory.getScriptEngine();
if (engine instanceof Compilable) {
try {
((Compilable) engine).compile("");
...
For velocity I get the following:
javax.script.ScriptException: org.apache.velocity.exception.ResourceNotFoundException: No template name provided
at org.apache.velocity.script.VelocityScriptEngine.compile(VelocityScriptEngine.java:311)
at org.apache.velocity.script.VelocityScriptEngine.compile(VelocityScriptEngine.java:288)
at com.Workers.LevelCheck.main(LevelCheck.java:69)
Caused by: org.apache.velocity.exception.ResourceNotFoundException: No template name provided
at org.apache.velocity.runtime.resource.loader.StringResourceLoader.getResourceStream(StringResourceLoader.java:353)
at org.apache.velocity.Template.process(Template.java:108)
at org.apache.velocity.script.VelocityScriptEngine.compile(VelocityScriptEngine.java:306)
... 2 more
Also when I tried to give template name ((Compilable) engine).compile("v.vm"); it failed with same exception
You cannot use velocity-engine-scripting-2.0.jar with velocity-1.7.jar, you need to use velocity-engine-core-2.0.jar otherwise you'll get unpredictable results.
When I call eval (in strict mode) on a nashorn engine with the following script I get an exception:
var yfunc = function () {
(null).apply(null, arguments);
};
yfunc();
I've truncated my personal situation heavily. The "(null)" on line 2 can be replaced with anything between parenthesis or a local variable, either way just something that shouldn't throw a compile error, and it will yield the same result.
The issue seems to be explicitly that "arguments" is passed directly as the second argument of calling a method called "apply". Any of the following changes will undo the thrown exception:
Putting "arguments" in a variable first (but simply wrapping it in parenthesis doesn't work!)
Calling something other than apply
Passing "arguments" in a different argument slot when calling apply
Calling print() (with or without passing any arguments) as a preceding line of code inside yfunc() (weird huh?)
Defining more than 0 parameters for yfunc()
Binding yfunc first and then calling the bound method
Calling yfunc via Function.apply (not so much with Function.call!)
The Exception thrown is this:
Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.runtime.Undefined to jdk.nashorn.internal.runtime.ScriptFunction
at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:4)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
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:264)
When I call this method with an owner, the exception thrown changes. Example code:
var yfunc = {
method: function () {
(null).apply(null, arguments);
}
};
var x = yfunc.method();
Then the thrown exception looks like this:
Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.scripts.JO4 to jdk.nashorn.internal.runtime.ScriptFunction
at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:5)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
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:264)
I've reproduced the issue so far on specifically these environments:
windows 7 64bit -> jdk 1.8.0_60 64bit
windows 8 64bit -> jdk 1.8.0_131 64bit
I can't seem to find anything on the internet about similar issues. Do I need to report this to Oracle/OpenJDK?
Minor update
Added items 6 and 7 to list of "following changes will undo the thrown exception".
Final update
Bug filed: JDK-8184720
Yes, it appears to be a bug. Please file a bug.
I have a problem while transforming the xslt to pdf in java, i am following the same process posted in this link
"Java Transformation to PDF".
Error:
`
Caused by: java.lang.NoSuchMethodError: org.apache.xmlgraphics.java2d.GraphicContext.<init>(Lorg/apache/xmlgraphics/java2d/GraphicContext;)V
at org.apache.fop.render.intermediate.IFGraphicContext.<init>(IFGraphicContext.java:50)
at org.apache.fop.render.intermediate.IFGraphicContext.clone(IFGraphicContext.java:56)
at org.apache.fop.render.intermediate.IFRenderer.saveGraphicsState(IFRenderer.java:632)
at org.apache.fop.render.intermediate.IFRenderer.startViewport(IFRenderer.java:885)
at org.apache.fop.render.intermediate.IFRenderer.startVParea(IFRenderer.java:878)
at org.apache.fop.render.AbstractRenderer.renderRegionViewport(AbstractRenderer.java:289)
at org.apache.fop.render.intermediate.IFRenderer.renderRegionViewport(IFRenderer.java:731)
at org.apache.fop.render.AbstractRenderer.renderPageAreas(AbstractRenderer.java:249)
at org.apache.fop.render.AbstractRenderer.renderPage(AbstractRenderer.java:230)
at org.apache.fop.render.intermediate.IFRenderer.renderPage(IFRenderer.java:580)
at org.apache.fop.area.RenderPagesModel.addPage(RenderPagesModel.java:114)
at org.apache.fop.layoutmgr.AbstractPageSequenceLayoutManager.finishPage(AbstractPageSequenceLayoutManager.java:312)
at org.apache.fop.layoutmgr.PageSequenceLayoutManager.finishPage(PageSequenceLayoutManager.java:167)
at org.apache.fop.layoutmgr.PageSequenceLayoutManager.activateLayout(PageSequenceLayoutManager.java:109)
at org.apache.fop.area.AreaTreeHandler.endPageSequence(AreaTreeHandler.java:238)
at org.apache.fop.fo.pagination.PageSequence.endOfNode(PageSequence.java:120)
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.endElement(FOTreeBuilder.java:349)
at org.apache.fop.fo.FOTreeBuilder.endElement(FOTreeBuilder.java:177)
at net.sf.saxon.event.ContentHandlerProxy.endElement(ContentHandlerProxy.java:391)
at net.sf.saxon.event.ProxyReceiver.endElement(ProxyReceiver.java:174)
at net.sf.saxon.event.NamespaceReducer.endElement(NamespaceReducer.java:213)
at net.sf.saxon.event.ComplexContentOutputter.endElement(ComplexContentOutputter.java:417)
at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:301)
at net.sf.saxon.instruct.Block.processLeavingTail(Block.java:409)
at net.sf.saxon.instruct.Instruction.process(Instruction.java:94)
at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:298)
at net.sf.saxon.instruct.Template.applyLeavingTail(Template.java:175)
at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:343)
at net.sf.saxon.Controller.transformDocument(Controller.java:1736)
at net.sf.saxon.Controller.transform(Controller.java:1560)
at mypackage.v2.business.pdf.XMLtoPDF.convertXMLPDF(XMLtoPDF.java:103)
... 51 more
java.lang.ClassCastException: java.lang.NoSuchMethodError cannot be cast to java.lang.Exception
`
Please let me know what could be the problem.
As described in the documentation
Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.
Here's how this can happen. Imagine you built some code that used a library with a method called foo(). You compiled your project and then later decided to upgrade the library. You do this by overwriting the old jar file with the new one. You don't recompile your code.
But what you didn't know is the new library removed the foo() method. Now if you run your code, it will throw this exception because your compile code calls this method and it's no longer there.
In your specific case, this isn't necessarily your code with the problem. It could be that you have one library that uses another library and the problem is there (for example, Library X uses version 2 of Library Y, but you only have Library Y version 1 in your classpath). If this is happening, you need to make sure the libraries are using the version they expect.
For your specific problem, you need to find the version of the jar that has org.apache.xmlgraphics.java2d.GraphicContext in it and it needs to have a constructor that takes a... list of org/apache/xmlgraphics/java2d/GraphicContext... maybe. I forget what (L...;)V means.
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.
I'm trying to implement java URL into my gwt project. This is what I've done so far in terms of experimenting, trial-error, etc:
HTML:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="Test_project.css">
<title>John Mathew's Google AJAX Search API Sample</title>
<script src="http://www.google.com/jsapi?key=*insert google api key*" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1');
google.setOnLoadCallback(function(){
var searchControl = new google.search.SearchControl();
searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
searchControl.addSearcher(new google.search.WebSearch());
var drawOptions = new google.search.DrawOptions();
drawOptions.setDrawMode(google.search.SearchControl.DRAW_MODE_TABBED);
searchControl.draw(document.getElementById("content"), drawOptions);
}, true);
</script>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load('search', '1', {language : 'en', style : google.loader.themes.SHINY});
google.setOnLoadCallback(function() {
var customSearchControl = new google.search.CustomSearchControl('*insert google cse id*');
CustomSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.draw('cse');
}, true);
</script>
<script type="text/javascript" language="javascript" src="test_project/test_project.nocache.js"></script>
Loading...
document.writeln('search engine 2');
Loading...
document.writeln('search engine 3');
<div id="customSearch"></div>
Now both parts of the javascript generates a google textbox and button. Upon entering information, it will return results. This isn't what I want. I would like to obtain the results for me to manipulate and play around with. So I did a bit of research and discovered PHP get_file_contents() and JAVA URL(). I've never had PHP experience so I went with Java (there is JQuery get() but I am confused on how to implement and couldn't find examples).
To understand how Java URL() works, I created a Java project and learned of this code:
URL urlsearch = new URL("*insert website*");
BufferedReader buffreader = new BufferedReader(new InputStreamReader(urlsearch.openStream()));
String HTMLdisplay;
while ((HTMLdisplay = buffreader.readLine()) != null) {
System.out.println(HTMLdisplay);
}
buffreader.close();
It works exactly how I wanted as it retrieves the html page source of a page (example: http://www.bing.com/search?q=gygax). To implement this, I created a project based on how http://code.google.com/webtoolkit/doc/latest/tutorial/create.html instructs thus in the HTML code above there is the line . The command that links the two is in the class onModuleLoad():
// Associate the Main panel with the HTML host page.
RootPanel.get("customSearch").add(mainPanel);
This sounds a bit confusing so I'll summarize a bit: using Google Widgets on my .java I created a textbox and button when I run it as a web application. When you click the button, it is suppose execute the java url code and retrieve the pagesource on what it queries. Instead I get these errors:
[DEBUG] [test_project] - Validating newly compiled units
[ERROR] [test_project] - Errors in 'file:/C:/*file path*/Test_project.java'
[ERROR] [test_project] - Line 92: No source code is available for type java.net.URL; did you forget to inherit a required module?
[ERROR] [test_project] - Line 93: No source code is available for type java.io.BufferedReader; did you forget to inherit a required module?
[ERROR] [test_project] - Line 93: No source code is available for type java.io.InputStreamReader; did you forget to inherit a required module?
[ERROR] [test_project] - Line 99: No source code is available for type java.net.MalformedURLException; did you forget to inherit a required module?
[ERROR] [test_project] - Line 102: The method exit(int) is undefined for the type System
[ERROR] [test_project] - Line 107: The method exit(int) is undefined for the type System
[TRACE] [test_project] - Finding entry point classes
[ERROR] [test_project] - Unable to find type 'com.example.test_project.client.Test_project'
[ERROR] [test_project] - Hint: Previous compiler errors may have made this type unavailable
[ERROR] [test_project] - Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
[ERROR] [test_project] - Failed to load module 'test_project' from user agent 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15 ( .NET CLR 3.5.30729; .NET4.0E)' at localhost:3410
I have import java.io.* and import java.net.* (have tried specific packages too) along with the many com.google.gwt packages.
MY QUESTION: How do I overcome these errors that prevent me from using JAVA URL in my GWT project?
you cannot use java.io.* or java.net in GWT Project client side. As GWT does not provide equivalents to the above packages (except Serializable).
I think Test_project.java is in client side only, so that you are getting this error.
In the client side you can use only lang,util packages.
Google Web Toolkit doesn't support the entire JRE standard library; it emulates only certain classes and unfortunately for you, it doesn't have java.net.*. It does however, have com.google.gwt.http.client. You should be using that.
You can make this work by moving the non-translatable code to a different location in your Java project.
Remember, GWT translates Java to javascript and runs in the browser. The GWT compiler targets anything beneath the module's client/ directory, that is. As no.good.at.coding mentioned, javascript lacks the java.io* and java.net* packages, so GWT can't make that part of your code into js.
However, you can use any library you want if you move the code into server-side .java files under server/rpc/ and expose it as a service. To wire up server-side rpc services with GWT, you will need to create a synchronous and asynchronous interface for your service, plus the implementation, e.g. (not checked for typos):
// rpc service interface, UrlGetServiceRpc.java:
#RemoteServiceRelativePath("urlget.rpc")
public interface UrlGetServiceRpc extends RemoteService {
String getPage(String url);
}
// async version of the interface, UrlGetServiceRpcAsync.java:
public interface UrlGetServiceRpcAsync {
void getPage(string url, AsyncCallback<String> async);
}
// implementation of the service, UrlGetServiceRpcImpl.java:
#Override
public string getPage(string url) {
// return page fetched using your code above.
}
// now in your page's module file... class level field:
private UrlGetServiceRpc urlGetSvc;
// in your page's constructor:
this.urlGetSvc = GWT.create(UrlGetServiceRpc.class);
// and when you want to display the html:
this.someWidget.setHtml(urlGetAsync.getPage("foo.com/goodreading.html"));
There are probably errors in the above, but it should get the general idea across. I also omit the import statements for incompatible packages from my GWT modules (client/* pages), and instead I use fully-qualified references in my methods (java.net.URL instead of just URL). This confines the scope of the non-GWTable package to the method level, and helps make sure the GWT compiler won't see the imports and whine.
Now you can also do get the html and display it asynchronously, by declaring the service as private UrlGetServiceRpcAsync urlSvc; in your page, then calling the Async overload of the method. That's a bit beyond the scope of this question though; Google's "Communicating with a Server" article is a great place to start with understanding what runs where in a GWT app, and how to isolate things into the right layer.