question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ClassAliasPool.forName0: "java.lang.ClassNotFoundException: byte[" error when using custom ClassLoader

See original GitHub issue

To reproduce the problem, put all necessary JARs to a temporary directory, like d:/temp/jars:

chronicle-algorithms-1.1.8.jar chronicle-bytes-1.7.34.jar chronicle-core-1.7.15.jar chronicle-map-3.13.0.jar chronicle-threads-1.7.7.jar chronicle-values-1.5.5.jar chronicle-wire-1.7.30.jar javapoet-1.5.1.jar slf4j-api.jar slf4j-nop.jar

Then have following “application” main class:

public class ApplicationMain {
	public static void main(String[] args) throws IOException {
		System.out.println("Opening and closing");

		ChronicleMapBuilder<byte[], byte[]> byteToByte =
				ChronicleMapBuilder.of(byte[].class, byte[].class)
						.name("sha-to-node")
						.entries(1).
						averageKeySize(20).
						averageValueSize(30);

		final File file = new File(args[0]);
		ChronicleMap<byte[], byte[]> map = byteToByte.createPersistedTo(file);
		map.close();
		map = byteToByte.createPersistedTo(file);
		map.close();
	}
}

and following “bootloader” main class:

public class BootLoaderMain {
	public static void main(String[] args) throws Exception {
		final List<URL> urls = new ArrayList<>();
		final File root = new File("d:/temp/jars");
		for (File file : root.listFiles()) {
			urls.add(file.toURL());
		}
		urls.add(BootLoaderMain.class.getResource("."));

		final ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
		final Class<?> mainClass = classLoader.loadClass("ApplicationMain");
		final Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
		mainMethod.invoke(null, new Object[] {new String[] {"d:/temp/chronicle"}});
	}
}

Running BootLoaderMain will result in following Exception:

Exception in thread "main" java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at BootLoaderMain.main(BootLoaderMain.java:29)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: net.openhft.chronicle.core.io.IORuntimeException: java.lang.ClassNotFoundException: byte[
	at net.openhft.chronicle.wire.Wires$SerializeJavaLang.lambda$apply$3(Wires.java:390)
	at net.openhft.chronicle.wire.ScalarStrategy.readUsing(ScalarStrategy.java:74)
	at net.openhft.chronicle.wire.ValueIn.object(ValueIn.java:494)
	at net.openhft.chronicle.wire.TextWire$TextValueIn.objectWithInferredType0(TextWire.java:2909)
	at net.openhft.chronicle.wire.TextWire$TextValueIn.objectWithInferredType(TextWire.java:2882)
	at net.openhft.chronicle.wire.ValueIn.object(ValueIn.java:435)
	at net.openhft.chronicle.wire.SerializationStrategies$9.readUsing(SerializationStrategies.java:201)
	at net.openhft.chronicle.wire.TextWire$TextValueIn.marshallable(TextWire.java:2633)
	at net.openhft.chronicle.wire.ValueIn.object(ValueIn.java:482)
	at net.openhft.chronicle.wire.TextWire$TextValueIn.objectWithInferredType0(TextWire.java:2909)
	at net.openhft.chronicle.wire.TextWire$TextValueIn.objectWithInferredType(TextWire.java:2882)
	at net.openhft.chronicle.wire.TextWire$TextValueIn.typedMarshallable(TextWire.java:2694)
	at net.openhft.chronicle.map.ChronicleMapBuilder.openWithExistingFile(ChronicleMapBuilder.java:1756)
	at net.openhft.chronicle.map.ChronicleMapBuilder.createWithFile(ChronicleMapBuilder.java:1555)
	at net.openhft.chronicle.map.ChronicleMapBuilder.createPersistedTo(ChronicleMapBuilder.java:1484)
	at ApplicationMain.main(ApplicationMain.java:17)
	... 10 more
Caused by: java.lang.ClassNotFoundException: byte[
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at net.openhft.chronicle.core.pool.ClassAliasPool.forName0(ClassAliasPool.java:118)
	at net.openhft.chronicle.core.pool.ClassAliasPool.forName(ClassAliasPool.java:107)
	at net.openhft.chronicle.wire.Wires$SerializeJavaLang.lambda$apply$3(Wires.java:388)
	... 25 more

However, when changing following line:

		final ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), null);

to:

		final ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]));

it will work as expected. AFAIU, there will be two different Class<byte[]>es and only the boot loader Class<byte[]> is handled correctly.

Motivation:

We are using a small “boot” ClassLoader to bootstrap our application once deployed (but not when developing and starting from the IDE). Now I have source code using Chronicle-Map which is working fine when started from the IDE: it’s using a ChronicleMap<byte[], byte[]> and has no problems accessing a test database file. However, when starting our deployed application, I’m getting above Exception.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rogeriovalentecommented, Sep 5, 2017

I’m facing a similar problem. When the file doesn’t exists there is no exception and the data is stored to file.

This is the interface

public interface EnderecoServicoInterface {

  int MAX_VALUE_LENGTH_URL = 400;
  int MAX_VALUE_LENGTH_CHAVE = 100;

  public String getChave();

  public void setChave(@NotNull @MaxUtf8Length(EnderecoServicoInterface.MAX_VALUE_LENGTH_CHAVE) String parChave);

  public String getUrlServicoDesenv();

  public void setUrlServicoDesenv(
      @NotNull @MaxUtf8Length(EnderecoServicoInterface.MAX_VALUE_LENGTH_URL) String parUrlDesenv);

  public String getUrlServicoHomologa();

  public void setUrlServicoHomologa(
      @NotNull @MaxUtf8Length(EnderecoServicoInterface.MAX_VALUE_LENGTH_URL) String parUrlHomologa);

  public String getUrlServicoProducao();

  public void setUrlServicoProducao(
      @NotNull @MaxUtf8Length(EnderecoServicoInterface.MAX_VALUE_LENGTH_URL) String parUrlProducao);
}

This is how I create the map:

    final ChronicleMapBuilder<String, EnderecoServicoInterface> webservicesurlBuilder =
        ChronicleMapBuilder.of(String.class, EnderecoServicoInterface.class)
            .name("webservicesurlmap")
            .averageKeySize(EnderecoServicoCache.averageKeySize)
            .entries(EnderecoServicoCache.nEntries);

ChronicleMap<String, EnderecoServicoInterface> map = webservicesurlBuilder..createPersistedTo(new File(System.getProperty("java.io.tmpdir") + File.separator + "webservicesurlmap"));

And after put all data in map the close() method is invoked. Every time that I try through createPersistedTo() method is throwing the exception below.

Caused by: net.openhft.chronicle.core.io.IORuntimeException: java.lang.ClassNotFoundException: EnderecoServicoInterface

Any tips?

0reactions
minborgcommented, Jan 3, 2022

I am closing this issue as it appears to be solved using the PR merged in Core. @rogeriovalente please raise a separate issue if you still have problems with your map.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - ClassNotFoundException while trying to load class from ...
In a real situation, my input is a byte[] array from front-end of my application and I would prefer not to build JAR...
Read more >
How to Fix ClassNotFoundException in Java - Rollbar
Common causes of the java.lang.ClassNotFoundException are using the following methods to load a class: Class.forName(); ClassLoader.
Read more >
ClassLoader (Java SE 12 & JDK 12 ) - Oracle Help Center
If name is not null , it must be equal to the binary name of the class specified by the byte array b...
Read more >
How to resolve java.lang.ClassNotFoundException in Java ...
1) When we try to load a class by using Class.forName() method and .class file or binary of class is not available in...
Read more >
Index (OpenHFT/Chronicle-Core 2.20.126 API) - javadoc.io
Makes sure all the jars etc in the current class loader have been added to the class path. addValue(byte) - Method in interface ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found