Service Loader Class In Java, Having Issue with META-INF/services folder - java

I'm stuck in service loader class(java) can anybody help, here is my code and pic attached with it.
see the pic to understand completely and tell me what's the issue
Interface Code
package ServiceLoader_.SL;
interface Account {
String getMessage();
}
Class implementing that interface
package ServiceLoader_.SL;
public class Message implements Account {
#Override
public String getMessage() {
return "Hello";
}
}
Main class
package ServiceLoader_.SL;
import java.util.ServiceLoader;
public class main {
public static void main(String[] args) {
ServiceLoader<Account> ac = ServiceLoader.load(Account.class);
Account ab = ac.iterator().next();
if(ac.iterator().hasNext()){
System.out.println("hay^");
}
}
}
Gives error that no such element found when trying to access ac.iterator().next()
Click HereTo See Image

As specified by the documentation of ServiceLoader, the provider-configuration file must be part of the classpath and for ServiceLoader<Account> will be resolved as
META-INF/services/ServiceLoader_.SL.Account
Based on what I can deduct from the image, your file seems to reside at
ServiceLoader_/META-INF/services/ServiceLoader_.SL.Account
which the ServiceLoader implementation is unaware of, thus it will not locate (and therefore not provide) the implementation class ServiceLoader_.SL.Message.
To fix this issue you must move the META-INF to the classpath root. Since I do not recognize the IDE shown in the image, I cannot say how one can do that though.

Related

Subclass obtained from Serviceloader throws AbstractMethodError

I am trying to make a Java app that can load plugins implementing an abstract class and am having an AbstractMethodError with the instances generated from ServiceLoader. The code is a bit heavy so I've made a simplification below.
First, I have an abstract class:
package stuff.TheAbstractClass;
public abstract class TheAbstractClass implements ClassInterface{
//Stuff happens
}
Which implements the below interface:
package stuff.ClassInterface;
public interface ClassInterface {
public String getClassName();
}
I have a service provider NotAbstractClass which extends TheAbstractClass and states so in a meta-inf/services folder:
package anotherstuff.NotAbstractClass;
public final class NotAbstractClass extends TheAbstractClass implements ClassInterface{
private String name = "Silent Bob";
#Override
public String getClassName() { return name; }
}
Then on the main application (which is actually a plugin inside another application), I want to find all classes which extend TheAbstractClass:
package stuff.TheApp;
import java.util.ServiceLoader;
public class TheApp {
private String name;
public final static TheApp INSTANCE = new TheApp();
private TheApp() {
ServiceLoader<TheAbstractClass> serviceLoader =
ServiceLoader.load(TheAbstractClass.class);
for (TheAbstractClass class: serviceLoader) {
name = class.getClassName;
}
}
My application does find NotAbstractClass. I know this since, in the for loop, I can do class.getName() and it'll give me anotherstuff.NotAbstractClass) but gives the error:
java.lang.AbstractMethodError: stuff.TheAbstractClass.getClassName()Ljava/lang/String;
I'm stumped. Any suggestion? Thank you, Pedro
According to the API for AbstractMethodError you get this:
Thrown when an application tries to call an abstract method. Normally,
this error is caught by the compiler; this error can only occur at run
time if the definition of some class has incompatibly changed since
the currently executing method was last compiled.
Just by looking at your code and your comment I see that this could only have happened at runtime.
If that is the case then:
some class has incompatibly changed since the currently executing
method was last compiled
I've tested your logic after some adjustments in a Java compatible form and I had no problems. The only thing that seems to be happening is a change in any of the subclasses of TheAbstractClass.
Another thing I did was to declare the dependencies using the dependency files in: resources/META-INF/services:
file: <full-package>.TheAbstractClass
content: <full-package>.NotAbstractClass
After this I had no problems.
It seems the issue wasn't in the code, but in the IDE (IntelliJ). I deleted all previously packaged jars and made new jars without changing anything and it magically worked... So it's an IDE bug, and not a language issue!
Thanks to #Joao and #hotzst for taking time to read however.
Best, Pedro

Multiple classes java

So i'm awfully new to coding but i quite like it, i'm really young so i have 0 experience on related stuff.
I'm watching this youtube series about java code and in this episode:
he creates another class and uses it in the main one but im on intelij(not eclipse as he is) and it gave me two errors saying java couldnt find the symbol (my second class);
my code:
package com.company;
public class Main {
public static void main(String[] args) {
tuna tunaObject = new tuna();
tunaObject.simpleMessage(null);
}
Second class:
public class tuna{
public void simpleMessage(){
System.out.println("Another class");
}
}
Your simple message method does not accept parameters, so don't try to pass in any. Instead of calling simpleMessage(null) simply call simpleMessage().
Also either make sure that the tuna class is located in the same package as your main class, or import the tuna class via an import statement above the Main class and below the package declaration. Even if the two source files are in the same physical directory, the Java compiler won't understand which class you are referring to unless you specifically define each class in the same package.
Adjust your second class to:
package com.company;
public class tuna{
public void simpleMessage(){
System.out.println("Another class");
}
}
Wecome to Java.
Maybe you can confirm first that the second class is located in the same package with the Main. And it is better to claim a class in First letter upper-cased format.

Need help setting up intellij java project with multiple .java files from scratch

Edited to restart question from scratch due to complaints. I am a newbie to this format and to intellij so please excuse...
I am building a project in intellij for class. This project imports jnetcap and uses it to process a captured pcap file. My issue is I have two class files I am trying to integrate. NetTraffic which is the user interface class, and ProcessPacket that actually reads in the packet and does the work.
I have tried to make a project and import ProcessPacket into NetPacket but have been unsuccessful so far. I am sure I am missing something simple in this process but I just can not find anything showing the proper way to do this.
I have gotten it working by making a package under the src directory and adding both files to that package. This doesn't require an import from the NetPacket class and seems to work but my worry is that I need to be able to run this from a linux command line. I have been working all semester so far with everything in one source file so it hasn't been an issue until now. I don't remember using packages in the past under eclipse to do this.
Can someone offer a step by step process on how to properly add these source files to my project so that I am able to import ProcessPacket into NetTraffic or will leaving like this in a package work fine?
The files in question reside in package named nettraffic in src directory.
NetTraffic.java
package nettraffic;
public class NetTraffic {
public static ProcessPacket pp;
public static void main (String args[]) {
pp = new ProcessPacket();
pp.PrintOut();
}
}
ProcessPacket.java
package nettraffic;
import org.jnetpcap.*;
public class ProcessPacket {
public ProcessPacket() {
}
public void PrintOut() {
System.out.println("Test");
}
}
Note there is no real functionality in these at this time. Just trying to get the class import syntax correct before continuing. Again while this seems to work as a package I want to have it done without using a package and importing ProcessPacket.java into NetTraffic.java.
public class NetTraffic {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
You're calling the PrintOut() method outside of any constructor or method or similar block (static or non-static initializer blocks...), and this isn't legal. Put it in a constructor or method.
public class NetTraffic {
public NetTraffic() {
ProcessPacket pp = new ProcessPacket();
pp.PrintOut();
}
}

When does classLoader load imports?

Assume, that I have class with static methods only. Will class loader load every imported class when loading class to memory? Or it will only load imports when a method from this would need access to it?
Question is whether class loader loads imports when the class is loaded to memory, or just before some methods want to use them.
If it is the first option I would probably need to divide some of my Util classes, to be more specialized.
I think you can test it as follows:
package pkg1;
public class Test {
static {
System.out.println("Hello 111");
}
public static void meth() {
System.out.println("Hello 222");
}
}
Test 1:
package pkg2;
import pkg1.Test;
public class Tester {
public static void main(String... args) {
Test t;
}
}
That prints nothing.
Test 2:
package pkg2;
import pkg1.Test;
public class Tester {
public static void main(String... args) {
Test.meth();
}
}
Prints:
Hello 111
Hello 222
So, just because you have imported a class does not mean the classloader will load the class into the memory. It loads it dynamically when it's used.
I don't claim to know a lot about the class loader, but if you're talking about import statements then the class loader is irrelevant.
Import statements exist purely to allow the developer to use short class names rather than the fully qualified name of each class referenced in the class being written. The compiler uses those import statements very early on to resolve the names of the referenced classes before a single line of bytecode is created.
In general, the static code block at the top of a class file with a report (i.e. a print statement ) will give you a good idea of when the loading happens in your particular application.
However, when dealing with corner cases, like dynamic classes, inner static classes, or classes off the classpath that are dynamically loaded, you will have to be careful - because these classes might actually be loaded MULTIPLE times in an application.

java annotations - problem with calling a locator class from a Vaadin Project

I'm not sure how to explain this without writing several pages so I hope the actual code is more expressive.
I've made a jar containing multiple annotation declaration similar to the following:
#Target(ElementType.PACKAGE)
#Retention(RetentionPolicy.RUNTIME)
public #interface MarkedPackage {
}
then I have made a test jar containing several classes in several packages and marked just one package with the above annotation (with package-info.java) like below:
#myPackage.MarkedPackage
package package.test.jar;
this jar had in its build path the jar containing the annotations.
then I made a static class that has a method (LoadPlugins) that retrieves a list with all the jars of a directory. Then it searches through the jars for the 'package-info' class and checks if that classes package contains the MarkedPackage annotation. by calling this:
if (checkPackageAnnotation(thisClass.getPackage()))
where thisClass is the package-info class retrieved via a classloader. and:
public static boolean checkPackageAnnotation(AnnotatedElement elem) {
System.out.println(elem.getAnnotations().length);
if (elem == null || !elem.isAnnotationPresent(MarkedPackage.class))
return false;
return true;
}
the elem.getAnnotatios().length is there for debug purposes.
And the problem appears when I call the method from the static class:
if I call it from a main function:
public class MyMain {
public static void main(String[] args){
PluginUtils.LoadPlugins();
}
}
everything works perfectly it displays '1' from that System.out.println(elem.getAnnotations().length);
But if I call it from a button from my Vaadin project:
header.addComponent(new Button("CallThat",
new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
PluginUtils.LoadPlugins();
}
}));
It displays '0' from that System.out.println(elem.getAnnotations().length);
Also I should mention that I created the main inside my Vaadin project so it would have the exact same build path and resources.
Is there a problem with web applications and that "#Retention(RetentionPolicy.RUNTIME)" ?
hope I was clear enough... Hope someone has a solution for me... If you need more information - let me know.
Thank you.
yes,
because there is only one package-info class in one package.

Categories