Ask previous question but in another form.
Class callapi contains method MainMethod(String name). I put this class in jar file callapi.jar.
Class staticapi calls method MainMethod from jar.
So question:
How to call method MainMethod(String name) from callapi.jar?
P.s staticapi class and callapi.jar are in staticapi.jar
import PACKAGE.callapi;
add the call as
new callApi().MainMethod(name_toBePassed);
PS:- the jar file should be in the class path of JVM
Related
I want to call a Class A from JAR-1. But I have same Class in JAR-2 hence my app is calling Class A from JAR-2.
All the packages are same in both the JARs. For Eg,
com.package.classes.A in JAR-1 and com.package.classes.A in JAR-2
I want my app to call A from JAR-1.
I am using IntelliJ.(If this information helps)
I would like to write java program that will print Class file name that Refer class inside JAR like below,
My jar file name : ExportExcel.jar it has class com/example/ExportXLSXExcel.class
below is my Java class that refer above class
import com.example.*;
Class DisputeProcess {
ExportXLSXExcel excel = new ExportXLSXExcel();
excel.download();
}
When I provide jar file name ExportExcel.jar. Java program should print DisputeProcess. Because DisputeProcess referred a class that inside ExportExcel.jar
I have a method M1 in Class C1 under the package P1 and which is under Java project called JP1. I am able to successfully exported jar file without any issues.
After adding the jar file to classpath in another project, I tried to call the method M1 under the package P2 under the Java project called JP2, but for some reason, I am not able to find the method "M1" since the package name P1 and P2 are different, If I maintain the P2 package name as P1 then i am able to call M1 method.
How to call the method if package names are different?
Note: I exported the jar file in Eclipse without any issues.
That means either the method, or the class the method is in (or if that class is in another class, that class, all the way until you get to a top-level class; one that is the top outer thing inside its own source file) is not marked public. Mark them as such; you have explicitly told java you do not wish for the construct to be visible outside of the package.
So, you have this:
class MyClass {
public void foo() {}
}
or this:
public class MyClass {
void foo() {}
}
(or combined; both the class and the method aren't public). You need this:
public class MyClass {
public void foo() {}
}
I want to use .jcall from the rJava package to call a custom java class method.
According to the following example from the rJava-documentation
.jcall("java/lang/System","S","getProperty","os.name")
I tried
.jcall("jrae/src/main/RAEBuilder.java","V","main")
Where "jrae/src/main/RAEBuilder.java" is the path I copied from the eclipse properties of the java class file, "V" represents a void return type and "main"
is the method I want to call from the RAEBuilder.java class.
However, the .jcall method returns with
RcallMethod: cannot determine object class
What could be wrong? (please be patient with me, I am a java-novice)
I found the problem:
I forgot to import the path of the class tree through
.jaddClassPath(path)
before I tried to call the class through .jcall.
In my main I have the following statement
Class booki = Class.forName("Book");
which throws a java.lang.ClassNotFoundException exception
when I use the full path like Class booki = Class.forName("javatests.Book"); it is ok.
The main class and the Book class are in the same package, I also tried using import static javatests.Book.*; but still it throws the exception if I don't set the full path javatests.Book. Can someone explain to me why?
Class.forName resolves a fully qualified class name to the class. Since a method does not know where it is called from neither the package of the calling class nor imports in the calling class play any role.
From docs Class#forName
public static Class<?> forName(String className)
throws ClassNotFoundException
Parameters:
className - the fully qualified name of the desired class.
So this will not throw ClassNotFoundException
Class booki = Class.forName("javatests.Book");
For example, it is not needed to import java.lang.* package in java program but to load class Thread from java.lang package you need to write
Class t = Class.forName("java.lang.Thread");
the above code fragment returns the runtime Class descriptor for the class named java.lang.Thread
You always need a qualified class name unless it's inside the same package. If i define a class foo in my package i can call a method Class testClass = Class.forName("foo") , but i can't call Class testClass = Class.forName("SecureRandom"); even if I import SecureRandom. That's just how the function works. It probably has a shortcut where it tries to find things inside local packages, but doesn't do much behind that.
Firstly the Book class must be in the package javatests.
the JVM load the class by name,through the classpath.
There is no class named "Book" in the classpath.
So JVM give you a ClassNotFoundException when excuse Class.forName("Book").
But 'Class.forName("javatests.Book")' tells JVM the class named 'Book' is in package 'javatests'.
So the JVM can find it and load it.
I hope my answer is helpful :)
JLS provides the following description:
Class lookup is always on behalf of a referencing class and is done through an instance of ClassLoader. Given the fully qualified name of a class, this method attempts to locate, load, and link the class.
The JDK uses one instance of ClassLoader that searches the set of directory tree roots specified by the CLASSPATH environment variable; and obviously it is not aware of the place (package) it has been called. That is why it needs fully qualified name.