使用node-java初始化我的java方法的问题

时间:2016-08-19 05:43:11

标签: java node-java

我不是java jock,但我正在尝试学习如何使用node-java。我试图运行npm java和gethub node-java上列出的一个示例,但它们不起作用。我认为我的方法没有在我的node.js脚本中正确初始化。我在Window 10笔记本电脑上使用JDK / JRE 1.8。下面是我的简单代码示例。任何帮助将不胜感激。

var Test = java.import("com.sample.SearchQueryRulesFromTable");

var result = Test.SearchQueryRulesFromTable("C1", "P1");
console.log(result);

node-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

1 个答案:

答案 0 :(得分:0)

TypeError: Could not find method
"com.sample.SearchQueryRulesFromTable(java.lang.String,   
java.lang.String)" on class "class com.sample.SearchQueryRulesFromTable".

此错误告诉您已尝试在类SearchQueryRulesFromTable上调用接受两个String参数的构造函数。该类没有定义这样的构造函数。

您的代码正在尝试在此处调用此不存在的构造函数:

SearchQueryRulesFromTable("C1", "P1");

根据您要执行的操作,您可能需要在main内调用SearchQueryRulesFromTable方法,并将其传递给String[]