java.util.ServiceLoader not working on WebSphere? no issues on Eclipse - java

I have a Java app (with no main) that runs well on eclipse (I have imported Okta Dependencies using Maven https://github.com/okta/okta-auth-java), but when uploaded to Tririga ClassLoader, which runs over WebSphere it throws this error:
java.lang.IllegalStateException: Unable to find a 'com.okta.sdk.impl.http.RequestExecutorFactory' implementation on the classpath. Please ensure you have added the okta-sdk-httpclient.jar file to your runtime classpath.
at com.okta.commons.lang.Classes.lambda$loadFromService$0(Classes.java:205)
at com.okta.commons.lang.Classes$$Lambda$46.000000002B3AE740.get(Unknown Source)
at java.util.Optional.orElseThrow(Optional.java:301)
at com.okta.commons.lang.Classes.loadFromService(Classes.java:205)
at com.okta.sdk.impl.client.BaseClient.createRequestExecutor(BaseClient.java:103)
at com.okta.sdk.impl.client.BaseClient.<init>(BaseClient.java:72)
Of course that I included okta-sdk-httpclient.jar to the ClassPath (and other dependencies).
When debugging the app on Eclipse, on com.okta.commons.lang.Classes, the variable 'return' obtains the value Optional[com.okta.sdk.impl.http.httpclient.HttpClientRequestExecutorFactory#5e955596]; but when running on WebSphere, I assume it is assigned with nothing as .orElseThrow is executed:
public static <T> T loadFromService(Class<T> clazz, String errorMessage) {
try {
ServiceLoader<T> serviceLoader = ServiceLoader.load(clazz);
Optional<T> result = StreamSupport.stream(serviceLoader.spliterator(), false).findFirst();
return result.orElseThrow(() -> new IllegalStateException(errorMessage));
} catch(ServiceConfigurationError e) {
throw new IllegalStateException(errorMessage, e);
}
}
WebSphere 9.0.5.0 (Latest as of 08-08-2019)
IBM Java JDK 8.0.5.37 (Latest as of 08-08-2019)
Tririga 3.6.0.4 (Latest as of 08-08-2019)
Not sure where to look, please help.

Related

Getting a specific version of an image with Jib (Maven, Docker, testcontainers)

I'm trying to understand a comment that a colleague made. We're using testcontainers to create a fixture:
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
public class SalesforceFixture extends GenericContainer<SalesforceFixture> {
private static final String APPLICATION_NAME = "salesforce-emulator";
public SalesforceFixture() {
// super(ImageResolver.resolve(APPLICATION_NAME));
super(DockerImageName.parse("gcr.io/ad-selfserve/salesforce-emulator:latest"));
...
}
...
The commented code is what it used to be. The next line is my colleague's suggestion. And on that line he commented:
This is the part I don't know. The [ImageResolver] gets the specific version of the emulator, rather than the latest. You need a docker-info file for that though, which jib doesn't automatically generate (but I think it can).
This is what I know or have figured so far:
SalesforceFixture is a class that will be used by other projects to write tests. It spins up a container in Docker, running a service that emulates the real service's API. It's like a local version of the service that behaves enough like the real thing that if one writes code and tests using the fixture, it should work the same in production. (This is where my knowledge ends.)
I looked into ImageResolver—it seems to be a class we wrote that searches a filesystem for something:
public static String resolve(String applicationName, File... roots) {
Stream<File> searchPaths = Arrays.stream(roots).flatMap((value) -> {
return Stream.of(new File(value, "../" + applicationName), new File(value, applicationName));
});
Optional<File> buildFile = searchPaths.flatMap((searchFile) -> {
if (searchFile.exists()) {
File imageFile = new File(searchFile + File.separator + "/target/docker/image-name");
if (imageFile.exists()) {
return Stream.of(imageFile);
}
}
return Stream.empty();
}).findAny();
InputStream build = (InputStream)buildFile.map(ImageResolver::fileStream).orElseGet(() -> {
return searchClasspath(applicationName);
});
if (build != null) {
try {
return IOUtils.toString(build, Charset.defaultCharset()).trim();
} catch (IOException var6) {
throw new RuntimeException("An exception has occurred while reading build file", var6);
}
} else {
throw new RuntimeException("Could not resolve target image for application: " + applicationName);
}
}
But I'm confused. What filesystem? Like, what is the present working directory? My local computer, wherever I ran the Java program from? Or is this from within some container? (I don't think so.) Or maybe the directory structure inside a .jar file? Or somewhere in gcr.io?
What does he mean about a "specific version number" vs. "latest"? I mean, when I build this project, whatever it built is all I have. Isn't that equivalent to "latest"? In what case would an older version of an image be present? (That's what made me think of gcr.io.)
Or, does he mean, that in the project using this project's image, one will not be able to specify a version via Maven/pom.xml—it will always spin up the latest.
Sorry this is long, just trying to "show my work." Any hints welcome. I'll keep looking.
I can't comment on specifics of your own internal implementations, but ImageResolver seems to work on your local filesystem, e.g. it looks into your target/ directory and also touches the classpath. I can imagine this code was just written for resolving an actual image name (not an image), since it also returns a String.
Regarding latest, using a latest tag for a Docker image is generally considered an anti-pattern, so likely your colleague is commenting about this. Here is a random article from the web explaining some of the issues with latest tag:
https://vsupalov.com/docker-latest-tag/
Besides, I don't understand why you ask these questions which are very specific to your project here on SO rather than asking your colleague.

NoSuchMethodError: com.google.common.cache.CacheBuilder.maximumSize(J)

I seem to be having a problem which only applied to 1 fellow user of a minecraft plugin of mine.
[15:54:14 ERROR]: Error occurred while enabling <Plugin> v1.0.8 (Is it up to date?)
java.lang.NoSuchMethodError: com.google.common.cache.CacheBuilder.maximumSize(J)Lcom/google/common/cache/CacheBuilder;
Is there any reason as to why this is happening or if I can over come it some way?
My code:
private LoadingCache<String, String> profileCache = CacheBuilder.newBuilder().
maximumSize(500).
expireAfterWrite(4, TimeUnit.HOURS).
build(new CacheLoader<String, String>() {
public String load(String name) {
try {
return getProfileJson(name);
} catch (IOException e) {
Bukkit.getLogger().info("Error, " + e.getLocalizedMessage() + ".");
}
return null;
}
});
You need to include the libraries into the exported jar. By default they are not included in the exported Jar as the IDE assumes they are present at runtime, which they are not.
See here for Gradle and here for Maven.
java.lang.NoSuchMethodError is thrown at runtime because the JVM does not find the method in the referenced class. This typically happens because you are using different versions of a third party library for compiling and running the application.
Check the version of the library used for compiling and the version used for running the code and make sure they match or are at least compatible.

Why do I get NoClassDefFoundError: java/awt/Desktop?

I'm trying to open an URI with Swing that I get above error.
What is the reason and how can I fix it?
When I do it in console everything is OK but when I do in GUI I get this error.
I should say that I use Weblogic as server.
Code
private static void open(URI uri) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e) { /* TODO: error handling */ }
} else { /* TODO: error handling */ }
}
Stack trace:
Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: java/awt/Desktop
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.open(BcfiDownloadPanel.java:230)
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.access$000(BcfiDownloadPanel.java:37)
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel$7.actionPerformed(BcfiDownloadPanel.java:147)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)
Doc on NoClassDefFoundError
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
You do have some incorrect classloading happening. Mostly due to wrong class loader chaining.
NoClassDefFoundError can only be caused by a classpath problem.
Because Desktop is part of jre, make sure that your classpath contains a reference to the jre library.
In Eclipse, you can go to run configurations --> Classpath and check there
UPDATE:
As Andrew suggested, you can also check you have at least java 1.6
java.awt.Desktop has been introduced in Java 6. Chances are high you're running your code on different JRE versions.

Problems compiling Eclipse project with Ant

I created a reasonably big web project on Eclipse (8 months of work). I've been using Eclipse build system until now. Now I'd like to go to Ant for a number of reasons (among them, be able to add certain pre WAR tasks, like js compression and other...). I discovered that Eclipse creates a build.xml file automatically, with all dependencies set up. The problem is that if I try to run it, it fails and gives this error:
type parameters of <TypeName>TypeName cannot be determined; no unique maximal instance exists for type variable TypeName with upper bounds TypeName,java.lang.Object
[javac] return dao.getItemByProperty(propertyName, val, objectClass);
it besically dies beacause of an error with generics.... usually it compiles fine on Eclipse (I know it is a different compiler...). How can I have javac work with this??
The method is:
#Override
#Transactional
public <TypeName> TypeName getItemByProperty(String propertyName,
Object val, Class objectClass) {
return dao.getItemByProperty(propertyName, val, objectClass);
}
and dao.getItem... is
#Override
public <TypeName> TypeName getItemByProperty(String propertyName,
Object val, Class objectClass) {
Session sess = sessionFactory.getCurrentSession();
Criteria criteria = sess.createCriteria(objectClass);
criteria.add(Expression.eq(propertyName, val));
#SuppressWarnings("unchecked")
List<TypeName> results = criteria.list();
if (results != null && results.size() != 0) {
TypeName res = results.get(0);
return res;
}
return null;
}
they are in two classes that respectively implement two interface, the first for a service, the second for a dao and they are used in Spring.
Why is this happening? Is Eclipse compiler so different from javac?
What version of Ant is actually being used, and which version of the compiler is being used? For a sanity check, try this at the command line where you are trying to run ant:
ant -v.
javac -v.
I ran into a similar situation once, where everything should work but didn't. Here Weblogic had an older version of both ant and javac than what I was trying to use, and these older version were being used instead of the ones I wanted. I ended up writing a script that explicitly set these variables in my PATH, and running the script before running the ant task.

Another odd NoClassDefFoundError on WhiteSpaceProcessor

I'm strugelling with an odd problem for days now.
Only one of the users of my webapp get an NoClassDefFoundError when trying to use some functionallity. This is the stacktrace:
java.lang.NoClassDefFoundError: com/sun/xml/bind/WhiteSpaceProcessor
at com.sun.xml.bind.DatatypeConverterImpl._parseInt(DatatypeConverterImpl.java:105)
at com.foo.bar.webservice.generated.GetLoginsRequest_JaxbXducedAccessor_panelId.parse(TransducedAccessor_field_Integer.java:32)
at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:166)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:406)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:384)
at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:35)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:224)
at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:107)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:289)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:272)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:106)
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:424)
On a strange way WhiteSpaceProcessor can't be found while it is on the classpath.
I used tattletale to look at the possitions of the usage of the classes:
WhiteSpaceProcessor only exist once on the classpath:
DatatypeConverterImpl only exist once on the classpath
I'm stuck on the fact that the exact war on a different environment is working perfect.
working environment:
Windows machine
Tomcat 5.5.28
Java 5 (jdk1.5.0.22)
none working environment:
Linux machine
Tomcat 5.5.??
Java 5 (jdk1.5.0.22)
I hope somebody can sent me in the right direction.
tomcat server is already restarted
Did you use tattletale on the working or non-working machine?
Perhaps the failing environment contains some jar file in jre/lib/ext (or a similar extensions directory), and that's being used in preference to a "lower down" version?
EDIT: Just to go into a bit more detail about the situations in which NoClassDefFoundError can be thrown, it's worth reading the JVM spec, chapter 5. It talks about three situations:
The resource corresponding to the class can't be found at all
The resource is found, but doesn't correspond to the right class (although in that case I'd expect a message including "wrong name")
You're using a version of Java earlier than 1.2, and the class file has an unsupported major/minor version number. (This situation now throws UnsupportedClassVersionError.)
Also read section 2.17.5: it states that if the class is in an "erroneous state" (e.g. previously initialization failed, or there was a bytecode verification failure) then NoClassDefFoundError will be thrown.
Now, if the static initializer of the class fails then the first caller sees an ExceptionInInitializerError - but the second caller sees NoClassDefFoundError. Here's a short but complete program to demontrate this:
class Foo {
static {
if (true) {
throw new RuntimeException();
}
}
static void foo() {
}
}
public class Test {
public static void main(String[] args) {
try {
Foo.foo();
} catch (Throwable t) {
System.out.println("First exception: " + t);
}
try {
Foo.foo();
} catch (Throwable t) {
System.out.println("Second exception: " + t);
}
}
}
Now unless something in your system is suppressing the ExceptionInInitializerError, I'd expect to see that in the log before NoClassDefFoundError if that were the problem. I still think it's more likely that your failing system is loading one class in an extension classloader which then can't find the ShiteSpaceProcessor class.
NoClassDefFoundError does not mean that the class file cannot be found in the classpath. It means that the class cannot be loaded. This is generally due to an error during initialization, or, more often, a version mismatch in JAR files on which the class depends.
Eg, you probably compiled against XYZ package version 1.2 and your user has XYZ version 1.1 installed.

Categories

Resources