![](/img/trans.png)
[英]Does node-java allow anonymous functions to be passed to java as parameters?
[英]problems initializing my java method using node-java
我不是Java专家,但我正在尝试学习如何使用Node-java。 我试图运行npm java和gethub node-java上列出的示例之一,但它们不起作用。 我认为我的方法未在我的node.js脚本中正确初始化。 我在Windows 10笔记本电脑上使用JDK / JRE 1.8。 下面是我的简单代码示例。 任何帮助,将不胜感激。
var Test = java.import("com.sample.SearchQueryRulesFromTable");
var result = Test.SearchQueryRulesFromTable("C1", "P1");
console.log(result);
节点Java错误
nodeJavaBridge.js:233
return java.newInstanceSync.apply(java, args);
^
TypeError: Could not find method
"com.sample.SearchQueryRulesFromTable(java.lang.String,
java.lang.String)" on class "class com.sample.SearchQueryRulesFromTable".
Possible matches:
public com.sample.SearchQueryRulesFromTable() at Error (native)
at javaClassConstructorProxy….
这是我的Java代码的一部分:
...
public class SearchQueryRulesFromTable {
...
public static final void main(String[] args) {...
javap的结果
Compiled from "SearchQueryRulesFromTable.java
public class com.sample.SearchQueryRulesFromTable {
public com.sample.SearchQueryRulesFromTable();
descriptor: ()V
public static final void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
}
@AlphaVictor我试图使用似乎无效的node-java构造来调用main方法。 我不知道我在做什么错。 以下是SearchQueryRulesFromTable中的主要方法:
ItemSearch item1 = new ItemSearch();
item1.setSearchCustomer(args[0]);
item1.setSearchItem(args[1]);
使用:
java.callStaticMethodSync("com.sample.SearchQueryRulesFromTable",
"ItemSearch(item1)","C1", "P1", function(err, results) {
if(err) {console.error(err);
javaLangSystem.out.printlnSync('test complete! '+ results);return;}
错误:
C:\Users\rdouglas\AppData\Roaming\npm\node_modules\NewProjects\08-
egilerapp1\classes>node test.js
(node) sys is deprecated. Use util instead.
C:\Users\rdouglas\AppData\Roaming\npm\node_modules\NewProjects\08-
egilerapp1\classes\test.js:14
var result =
java.callStaticMethodSync("com.sample.SearchQueryRulesFromTable",
"ItemSearch","C1", "P1")
^
错误:
Could not find method "ItemSearch(java.lang.String, java.lang.String)" on
class "class com.sample.SearchQueryRulesFromTable". No methods with that
name.
at Error (native)
at Object.<anonymous>
(C:\Users\rdouglas\AppData\Roaming\npm\node_modules\NewProjects\08-
egilerapp1\classes\test.js:14:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
TypeError: Could not find method
"com.sample.SearchQueryRulesFromTable(java.lang.String,
java.lang.String)" on class "class com.sample.SearchQueryRulesFromTable".
该错误告诉您,您试图在接受两个String
参数的类SearchQueryRulesFromTable
上调用构造函数。 在该类上没有定义此类构造函数。
您的代码正在尝试在此处调用此不存在的构造函数:
SearchQueryRulesFromTable("C1", "P1");
根据您要尝试执行的操作,您可能希望改为在SearchQueryRulesFromTable
调用main
方法,然后将其传递给String[]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.