problems initializing my java method using node-java - java

I'm not a java jock, but I'm trying to learn how to use node-java. I tried to run one of the examples listed on npm java and gethub node-java and they don't work. I think my method is not initialize in my node.js script correctly. I'm using JDK/JRE 1.8 on a Window 10 laptop. Below is my simple code example. Any help would be appreciated.
var Test = java.import("com.sample.SearchQueryRulesFromTable");
var result = Test.SearchQueryRulesFromTable("C1", "P1");
console.log(result);
Error in 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….
Here is part of my java code:
...
public class SearchQueryRulesFromTable {
...
public static final void main(String[] args) {...
Results of 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 I have tried to call the main method using node-java constructs that didn't seem to work. I don't know what I'm doing wrong. Below is the main method in SearchQueryRulesFromTable:
ItemSearch item1 = new ItemSearch();
item1.setSearchCustomer(args[0]);
item1.setSearchItem(args[1]);
Using:
java.callStaticMethodSync("com.sample.SearchQueryRulesFromTable",
"ItemSearch(item1)","C1", "P1", function(err, results) {
if(err) {console.error(err);
javaLangSystem.out.printlnSync('test complete! '+ results);return;}
Error:
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")
^
Error:
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".
This error tells you that you have tried to call a constructor on class SearchQueryRulesFromTable that accepts two String arguments. There is no such constructor defined on that class.
Your code is attempting to call this non-existent constructor here:
SearchQueryRulesFromTable("C1", "P1");
Depending on what you are trying to do, you may want to call the main method within SearchQueryRulesFromTable instead, and pass it a String[].

Related

Double colon operator not working with Java

just trying out things in java, found following issue.
DefaultAndStaticMethodMain.java:8: error: not a statement
implementation1::sendNotification;
^
1 error
Following is my code.
ParentInterface:
public interface ParentInterface {
default void callForCompletion() {
System.out.println("<<<< Notification sending completed. >>>>");
}
}
Child interface:
public interface ChildInterface extends ParentInterface {
public abstract void sendNotification();
static String printNotificationSentMessage() {
return "Notification is sent successfully.";
}
}
Implementation 1:
public class Implementation1 implements ChildInterface {
#Override
public void sendNotification() {
System.out.println("Implementation --- 1");
System.out.println("Sending notification via email >>>");
}
}
Implementation 2:
public class Implementation2 implements ChildInterface {
#Override
public void sendNotification() {
System.out.println("Implementation ---- 2.");
System.out.println("Sending notification via SMS >>>");
}
}
Main method:
public class DefaultAndStaticMethodMain {
public static void main(String[] args) {
Implementation1 implementation1 = new Implementation1();
implementation1::sendNotification; // Compilation error as shown above.
Implementation2 implementation2 = new Implementation2();
implementation2.sendNotification();
// Following works fine.
// Arrays.asList(implementation1, implementation2).stream().forEach(SomeInterfaceToBeRenamed::sendNotification);
}
}
I am not sure what am I doing wrong, I have JDK 13 installed in local machine and working with IntelliJ 2019.3 with JDK 11. I checked that IntelliJ supports JDK 13
Thanks.
Update
By mistake I left a semi-colon over there, removed it, please check again.
A method reference is not the same as a method call. Those are two distinct things.
A method call is a standalone expression, or, more precisely, an expression statement. That means that in your case implementation2.sendNotification() works, as you would expect.
A method reference, however,
is used to refer to the invocation of a method without actually performing the invocation
and is not a standalone expression. It can only be used where a lambda expression can also be used. A method reference as a standalone expression does not compile, just like an arithmetic expression without assignment (e.g. 3 + 17;). This is enforced by the Java Language Specification, § 14.8 and § 15.13.
More to read:
Please Explain Java 8 Method Reference to instance Method using class name
Method reference in Java 8
What do you intend for the implementation1::sendNotification; line to do? Judging by the implementation2.sendNotification(); line below it looks like you're trying to call sendNotification on implementation1, which is written like this:
implementation1.sendNotification();
The :: notation is a method reference, and (as the error message says) it an identifier, not a statement, and thus can't be a line on its own. Similarly you couldn't write implementation1; (a variable) or ChildInterface; (a class identifier) as a statement.
The .forEach(SomeInterfaceToBeRenamed::sendNotification); line compiles because you are passing the method reference to forEach(), and it in turn invokes each sendNotification() method.

Calling Java Code in C# using IKVM results in System.TypeInitializationException error

I am currently trying to call Java Code in C#. One possibility is IKVM, whereupon I looked at a tutorial for this tool. I have to say, and that's really curious: the tool seems to work in part.
But now to my problem:
So I took the following tutorial (https://www.codeproject.com/Articles/594632/IKVM-NET-in-Details). Following this example, I wrote my own Java code. In addition, I have added a few more methods to the java file. My source code for testing is relatively short:
The Java source code:
package TestProject;
public class TestClassJava {
public static void Print() {
System.out.println("Hi C# from JAVA");
}
public static void PrintStr(String str) {
System.out.println(str);
}
public static String returnString() {
return "Hi C# from Java method";
}
public static String returnInputString(String input) {
return input;
}
public static int retInt() {
return 42;
}
public static int returnIntNumber(int inp) {
return inp;
}
public static boolean returnTrueBoolean() {
return true;
}
}
The C# source code:
using System;
using System.IO;
using TestProject;
using ikvm.io;
using ikvm.lang;
using ikvm;
using ikvm.runtime;
using ikvm.extensions;
namespace IKVM_Test_Case_08_08_2019
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(TestClassJava.retInt()); // shows: 42 (works!)
Console.WriteLine(TestClassJava.returnString()); // shows: Hi C# from Java method (works!)
TestClassJava.Print(); // here appears the error System.TypeInitializationException
TestClassJava.PrintStr("Hallo"); // here appears the error System.TypeInitializationException
Console.WriteLine(TestClassJava.Print()); // can not convert from void to bool
}
}
}
The whimsical part now happens while running the program in C#. I try the methods in C# via Console.WriteLine(TestClassJava.retInt()); then, for example, the number 42 will be given to me, as it should be. I can also call the method returnString().
In the methods without return value, however, Print() & PrintStr(String str), I always get the following error message:
Error message:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'java.lang.StdIO' threw an exception.
Source=IKVM.OpenJDK.Core
StackTrace:
at java.lang.System.get_out()
at TestProject.TestClassJava.Print()
at IKVM_Test_Case_08_08_2019.Program.Main(String[] args) in C:\Users\...\source\repos\IKVM_Test_Case_08_08_2019\IKVM_Test_Case_08_08_2019\Program.cs:line 19
Inner Exception 1:
MissingMethodException: Method not found: 'Void System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.Security.AccessControl.FileSystemRights, System.IO.FileShare, Int32, System.IO.FileOptions)'.
I can not quite explain that, so I asked this question in the hope, that someone knows an advice.
According to the quoted tutorial, it all had to work that way. Nevertheless, I get this error message.
I hope my question is so far understandable.

isInstance() ClassNotFoundException

Following is my code which compiles fine but I am getting ClassNotFoundException for case 2 while running, my question is why I am getting error for case 2 :
Case 1
command java Var Var
output false
Case 2
command java Var Object
output ClassNotFoundException : Object
class Var
{
public static void main(String[] args) throws Exception
{
Thread t = new Thread();
System.out.println(Class.forName(args[0]).isInstance(t));
}
}
The fully qualified class name is java.lang.Object.
The compiler automatically imports java.lang.*, that's why you don't have to import classes from this package. However if you call Class.forName you have to use the fully qualified class name.

Not executing while running the java program in geany

I don't think there are any problems in my java code since I ran the program properly in BluJ, but I use geany extensively instead. However, a strange problem occurred today while building the program in geany. I have defined the main method properly in my code, but despite my program got compiled without any errors, while running it after executing the program in geany I got this error:
Error: Main method not found in class Ter, please define the main method as: public static void main(String[] args) or a JavaFX
application class must extend javafx.application.Application
My program is:
public class Ter {
public static void main(String[] args) {
int scored=3;
int concede=5;
char result = (scored > concede) ? 'W':'L';
System.out.println(result);
}
}

Groovy No signature of method calling Java library

As many questions begin, this is driving me crazy.
I have a homegrown StarTeam java library. I have one static method like this:
public static Label getLatestDeploymentLabel(com.starbase.starteam.File child) {
// blah
}
The method works as expected when I call it from java. When I call it from Groovy, I get:
Caught: groovy.lang.MissingMethodException:
No signature of method: static pkg.starteam.StarTeamUtils.getLatestDeploymentLabel()
is applicable for argument types: (com.starbase.starteam.File)
values: [FILENAME-FOO.sql] at starteam.run(starteam.groovy:54)
I put in a println right before I call that method:
chgset.elements().each() { item ->
println "type of item is ${item.class.getName()}"
def latestlabel = StarTeamUtils.getLatestDeploymentLabel(item)
}
And confirm that, in fact, it's iterating what I expect it's iterating over:
type of item is com.starbase.starteam.File
I've seen a few different similar issues in other posts relating to static methods and the responses are along the lines of "are you sure it's a static method?". I'm sure it's a static method.
There isn't much groovy code to this. What there is of it is all contained in a single script in the default package. The main method is then called implicitly and it's in the body of the script class that the call out to the java library is made. I set the classpath in a DOS batch wrapper script, e.g.:
SET INITIALCLASSPATH=%CLASSPATH%
SET NEWCP=c:/libs/etc.jar;c:/etc/etc.jar
SET GROOVYPATH=c:/groovy.bat
SET CLASSPATH=%NEWCP%
%GROOVYPATH% %*
SET CLASSPATH=%INITIALCLASSPATH%
I created a simple situation which I think emulates my situation.
C:\apps\groovy-1.8.6\scripts>type Other.java
class Other {
private String name = "notset";
public Other(String name) {
this.name = name;
System.out.println("Created an other");
}
public String toString() {
return name;
}
}
C:\apps\groovy-1.8.6\scripts>type ThingList.java
import java.util.ArrayList;
import java.util.Iterator;
class ThingList {
ArrayList ourlist = new ArrayList<Other>();
public ThingList(){}
public ArrayList add(Other thing) {
ourlist.add(thing);
return ourlist;
}
public Iterator iterator(){
return ourlist.iterator();
}
}
C:\apps\groovy-1.8.6\scripts>type JavaLib.java
class JavaLib {
public JavaLib() {}
public static ThingList getThingList(Other thing) {
ThingList tl = new ThingList();
Other one = new Other("extra one");
tl.add(thing);
tl.add(one);
return ThingList;
}
}
C:\apps\groovy-1.8.6\scripts>type testthing.groovy
def myOther = new Other("A new other")
println "type of myOther is ${myOther.class.getName()}"
def myList = getThingList(myOther)
myList.each() {
println it
}
C:\apps\groovy-1.8.6\scripts>type wrapper.bat
#ECHO OFF
SET INITIALCLASSPATH=%CLASSPATH%
SET GROOVY=C:\apps\groovy-1.8.6\bin\groovy.bat
SET CP=.
SET CLASSPATH=%CP%
%GROOVY% %*
SET CLASSPATH=%INITIALCLASSPATH%
C:\apps\groovy-1.8.6\scripts>wrapper.bat testthing.groovy
Created an other
type of myOther is Other
Caught: groovy.lang.MissingMethodException: No signature of method: testthing.ge
tThingList() is applicable for argument types: (Other) values: [A new other]
groovy.lang.MissingMethodException: No signature of method: testthing.getThingLi
st() is applicable for argument types: (Other) values: [A new other]
at testthing.run(testthing.groovy:3)
C:\apps\groovy-1.8.6\scripts>
Any insights or suggestions would be greatly appreciated!
AndyJ
Without a way to reproduce, it's impossible to say for sure what the problem is. One possibility is that it is a class loading problem. Is the Groovy code contained in a regular Groovy class that's sitting on the class path, or does the Groovy code get loaded dynamically (e.g. by using GroovyShell)?

Categories