FAQ Database Discussion Community


Can't access resources folder in Java

java,gradle,resources,classpath,classloader
I tried to access resources (marked as) folder in Java app but nothing works, returns wrong paths I've tried: ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); System.out.println(classLoader.getResource(".")); and ClassLoader classLoader = Config.class.getClassLoader(); System.out.println(classLoader.getResource(".")); but it shows me file:/D:/testProject/build/classes/main/, and of course there's no resource files :( How to access exactly resources folder? -TestProject...

ClassNotFoundException on android blank activity

java,android,android-studio,classloader,classnotfoundexception
I am having a problem with Android Studio (1.3 dev channel). I created the default blank activity from the menu (File->New->New project, and then API 16 as minimum SDK, Blank Activity, default settings). I wanted to test, with such a simple setup, if I can load the class called MainActivity...

Running a compiled java .class file from within java code and capturing output

java,classloader,javacompiler
I'm trying to write a java program with an interface that allows the user to create .java file and compile and run it (essentially a very simplistic IDE). I'm using java swing for the gui and have so far been able to compile a .java file from within the interface...

ClassLoader always returns null when called from within a jar

java,maven,classloader
I ran into library loading problems after creating a jar from my code via maven. I use intelliJ idea on Ubuntu. I broke the problem down to this situation: Calling the following code from within idea it prints the path correctly. package com.myproject; public class Starter { public static void...

Load resource file outside jar

web-applications,jar,classpath,classloader,web-inf
I have created a jar which contains a class that loads an external resource file from class path of my web application. My application is running on resin server. I have deployed my jar in web-inf/lib and my resource file is in web-inf/classes folder. Now the resource file is being...

Run each JUnit Test with a Separate ClassLoader (no, really)

java,junit,classloader,test-runner
How can I have JUnit use a separate ClassLoader for each test class it executes? I am writing a JUnit TestRunner for a library that sets a lot of static variables. I essentially want to reset all of these between each test class, without needing to know what all of...

How do I print the current classpath for a Jenkins plugin?

java,jenkins,classpath,classloader
I'm developing a Jenkins plugin but I'm having a few problems which I think might be due to a jar missing in the plugin's classpath even though its in the plugin's WEB-INF/lib directory. Normally I would use the following code in Java to print the classpath but this doesn't seem...

ClassLoader: Treating a class as a resource - safe or implementation detail?

java,classloader
For my use case, I want to transfer a class (not an object, the actual class file) over a Socket connection, then load that class on the receiving end. The class is included normally in the project (on the sending end), so its somewhere on the class path. I have...

Access resource from inside jar

java,jar,classloader
I got a jar file test.jar that contains a folder resources which contains txtFile.txt. I'm trying to access the file but the file seems to be null. package main; import java.net.URL; public class Test { private Test() { URL file = this.getClass().getClassLoader().getResource("/resources/txtFile.txt"); System.out.println(file == null); } public static final void...

Is JDK ClassLoader.getResourceAsStream broken? (unclosed resources)

java,resources,classloader,java-8,resource-leak
I will try to prove that ClassLoader.getResourceAsStream() is opening two InputStreams, closing none of it and returning only one to client. Is my logic correct? JDK sources are picked from jdk1.8.0_25 I've get into unclosed resources problem using Spring ClassPathResource in interval (original question), that is using ClassLoader.getResourceAsStream to get...

Loading an array with a classloader

java,class,classloader,loader
I'm trying to run this code: public class ClassLoaderTest { public static void main(String[] args) throws Exception { Object[] obj = new Object[]{}; String cname = obj.getClass().getName(); System.out.println(cname); ClassLoaderTest.class.getClassLoader().loadClass(cname); } } But it throws a ClassNotFoundException. Interestingly, if I instead use this line: Class.forName(cname); it works just fine. Whats going...

Javamail ClassCastException when sending multipart messages

java,jboss,jax-ws,javamail,classloader
I am getting the following exception when attempting to send a multipart message: java.lang.ClassCastException: com.sun.xml.internal.ws.encoding.StringDataContentHandler cannot be cast to javax.activation.DataContentHandler at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:609) at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:563) at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:625) at...

Rome 0.9 does not work correctly when module classloader order : parent last

websphere,classloader,rome
Project description: WebSphere Application Server 7.Maven project which uses Rome0.9. <dependency> <groupId>rome</groupId> <artifactId>rome</artifactId> <version>0.9</version> </dependency> I was solving the problem with log4j not logging. The problem was that log4j.properties were already set in parent project. That's why I changed module's classloader order to Parent Last. It fixed the problem with...

Different Java bytecode decompile to exactly the same p-code and java code

java,classloader,bytecode,java-bytecode-asm
I have generated two java bytecode files 1.class and 2.class, they are different if using hex file viewer inspect them: However the decompiled p-code are all the same: 1.class -> 1.java 2.class -> 2.java I can also use jd-gui to decompile them into Java source code and again the two...

I can't use Autoloader from Symfony2 in Symfony 1.4 for load this namespaced classes

namespaces,classloader,symfony-1.4,autoload
I want to use this php library with namespaced classes in my Symfony 1.4 project: https://github.com/donquixote/cellbrush. I'm not quite familiar with the namespaces concept. So when i fisrt try the to use the main class of this library, according to its docs, i just did: $table = \Donquixote\Cellbrush\Table\Table::create(); And i...

How does Java link lib/rt.jar to your app at runtime?

java,linker,jvm,classloader
The Java standard/system libraries (java.*, javax.*, etc.) are stored in lib/rt.jar inside each JRE distribution. Say I have an application that I have compiled and jarred into myapp.jar. This JAR only contains my app's class files, and merely references system classes like System, File, Runtime, Thread, String, Boolean, etc. So...

Why doesn't java's -verbose:class argument include jre inherent native classes?

java,class,jvm,classloader,native
When I use the VM argument "-verbose:class", it shows all classes that are being loaded except for those that are both inherent to java and declared natively, like primitives or arrays (can't think of anything else though, are there?). Despite using the following line of code within a main method,...

Java 8 ScriptEngine across ClassLoaders

java,java-8,classloader,nashorn
I need to execute some javascript code 'inside' different classloaders. If it is java, each task will run in separate class loader. Now I need this to be javascript. Do I need to create new instance of ScriptEngine in each classloader, or is it ok to share one across class...

Custom classloader trouble with getResources for names ENDING in slash

java,classpath,classloader,urlclassloader,dynamic-class-loaders
I am desperate for help but was unable to find anything on the web about this particular subject (many related ones that leave my particular problem unanswered). Specifically, I need to be able to download code (jars) from a central and external code repository. This is done by the bootstrap...

InvokeExact on the object, whose type is dynamically loaded by classloader

java,casting,classloader,methodhandle,invokevirtual
I have spend whole day on this problem. My problem is how to make an MethodHandle.invokeExact invocation on an instance, whose class type is dynamically loaded at program runtime. To make problem more clear, i show my sample code below: Class<?> expClass = new MyClassLoader().load(....) //expClass is AddSample.class which is...

Fail to load a dynamically generated bytecodes

java,classloader,java-bytecode-asm,bytecode-manipulation
I have succeeded in dumping dynamically generated bytecodes to a file using ASM, but fail to load it. The error message is: java.lang.ClassFormatError: JVMCFRE102 field signature invalid; class=TGWD, offset=0 at java.lang.ClassLoader.defineClass(ClassLoader.java:364) at java.lang.ClassLoader.defineClass(ClassLoader.java:284) at java.lang.invoke.ByteCodeClassLoader.run(ByteCodeClassLoader.java:20) The BytecodeClassLoader is a Class I wrote here. The javap -v result is also shown...

In which memory is saving Java ClassLoader instance

java,heap,classloader,permgen
I want to know where the ClassLoader instance is saving, in which memory. Is it saving in heap or in permgen? And if I have a custom ClassLoader( for example JasperReports loads his own ClassLoader which calls JRClassLoader ), instance of it is also saving in heap memory or in...

ClassLoader classes are not properly loaded in the thread

java,jar,jboss,casting,classloader
I am creating a new classLoader using URLClassLoader and trying to set it as the classLoader for the current thread. But it is not working properly for me. As per my understanding, if I set a classLoader to the current thread, the methods and interfaces referenced by the Current Thread...

java ResourceBundle.getBundle() undeterministic

java,classloader,java-ee-6
I have a very, very strange situation happening on our Java EE app running on WebSphere. EAR --someJar.jar ----config/propfile.properties --WAR ----WEB-INF ------classes --------config/propfile.properties When the application start up, ResourceBundle.getBundle("config/propfile.properties") seems to read someProperty from the WAR/WEB-INF/classes/config/propfile.properties. However, after the app is up and running, ResourceBundle.getBundle("config/propfile.properties") seems to read someProperty from...

How do I avoid problems arising from accessing static fields before the class is initialized?

java,static,classloader
I have this code: public abstract class Person { public static final class Guy extends Person { public static final Guy TOM = new Guy(); public static final Guy DICK = new Guy(); public static final Guy HARRY = new Guy(); } public static final List<Guy> GUYS = ImmutableList.of(Guy.TOM, Guy.DICK,...

Replace the class of a field at runtime (for protostuff)

java,reflection,classloader,protostuff
I'm working on a framework for backwards compatiblity between different versions of a class (from serialized binary representations). One thing I'm stuck on is how to replace a field used in a class with a different version of the field's class - at runtime. I know how I could do...

Java: find classes with certain base type on program startup

java,eclipse,classloader
I have a Java program (an Eclipse plug-in to be specific), which is using several JAR files (which I can control). One of the core JAR files defines an abstract class called Strategy. When the user starts the program, the program needs to know all sub classes of Strategy, that...

Launch Activity from Fragment throws ClassNotFoundException

java,android,android-fragments,classloader,classnotfoundexception
I know it's a long shot but I tried a lot of solutions and none worked. I'm trying to launch an activity from a fragment when a button is tapped. Fragment.java public class Lev1 extends Fragment implements OnClickListener { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View v =...

requirejs circular dependencies and function objects

javascript,requirejs,classloader,circular-dependency
Is it possible to differentiate between dependencies which are needed on structure build (like inheritance) and runtime dependencies (within a method call). A little example: 2 "classes": Father and Child which depend on each other Father.js define(['Child'], function (Child) { function Father() {}; Father.prototype.childs = []; Father.prototype.addChild = function (c)...

Java Applet - Cannot inherit from final class

java,applet,classloader,final
We have a java applet which is working OK in most client environments, primarily Windows 7, but recently we have been asked to support Ubuntu clients as well. The problem is that when the applet is fired up on the Ubuntu client (running Firefox and the natively installed "IcedTEA" Java...

How to understand ā€œEvery Class object contains a reference to the ClassLoader that defined it. ā€?

java,class,jvm,classloader
I know I can get a the classloader of a class by xxxclass.class.getClassLoader(), but where exactly does the xxxclass hold the reference of its classloader who defines it? e.g. public class ClassA { System.out.println("I am class A"); } I don't see any clue of the classloader reference in ClassA....

overriding findResource

java,resources,bytearray,classloader
I have a custom ClassLoader and I want getResource to look for resources at a custom location. Therefore, What I want to do is to override findResource, as I want it to return an array of bytes as the result. The return type of the findResource function is URL. So...

Recursive initialization : Static Initializer not getting called when i access Class field

java,static,classloader
Understanding about loading a class and calling static initializer In what order do static initializer blocks So, I just tried to confirm it - public class OOMErrorB extends OOMErrorA { public static int c = 10; static { System.out.println("Loading static B " + c); System.out.println(OOMErrorA.a); } public static void main(String[]...

java.util.prefs.Preferences uses SystemClassloader resulting in classloading issues

spring-boot,classloader
Trying to load nested jar classes via ClassLoader.getSystemClassLoader(), which the java.util.prefs.Preferences uses SystemClassloader in factory is doing results in errors with Spring-boot web app that is using a custom java.util.prefs.Preferences application. Is there a way i could make a nested jar class availabe to a system class loader?...

Eclipse/Java can't start any project

java,eclipse,classloader,launcher
I was working on my Java Code without problems. Then I updated to Java 8 because of that I could not start Eclipse anymore. So I downgraded it again to Java 7. Now I can start Eclipse but if I try to start any Java project I get this: Thread...

How use two verions of itext jar files in pom.xml?

java,maven,jar,maven-2,classloader
In my project i have dependencies displaytag and birt-runtime. And here display tag needs itext-2.0.1.jar and birt-runtime needs itext-2.1.7.jar so how can i declare this in my pom.xml to use itext-2.0.1.jar only by displaytag and use itext-2.1.7.jar only by birt-runtime. could some one let me know how to achieve this?...

Reading an EAR's manifest

java,java-ee,glassfish,classloader,manifest
I need to load the manifest of an application I am deploying on Glassfish as an EAR. All the info I've found on internet is regarding reading manifests of jars or wars. I've tried to apply the snippets of code I've found to my problem but I've not made it....

Dynamically Loading a pre-compiled class without reflection

java,reflection,classloader
In my specific case I'd like to load a class in runtime where: I have the class (and its definition) at compile time but I only find out the location of its containing jar at runtime. Somewhat reversed from the classic classloading case where the name of the class is...

Unexplainable ClassNotFoundException in server unmanaged thread

java,classloader,classnotfoundexception,websphere-8,apache-commons-io
I am facing a weird ClassNotFoundException: org.apache.commons.io.IOUtils under the following circumstances: I have a web application running in Websphere Application Server 8.0 that uses commons-io 2.0.1 (this jar is correctly placed in the classpath). I am using org.apache.commons.io.input.Tailer to tail a log file and show its contents on screen. Tailer...

Creating instances of loaded classes

java,classloader
I am quite new to the class loader problematic and have a question whether this would be possible: I have a class in a class file (compiled, no src code) - Hidden.class. I have a custom class loader which is able to load the class like this: CustomClassLoader loader =...

Struts startup failed due to logging errors

struts2,log4j,classloader,slf4j,nosuchmethoderror
Struts application can not start with log4j. What is wrong? Here is log4j.properties # Direct to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # Root logger option log4j.rootLogger=INFO, stdout # Good for troubleshooting log4j.logger.org.hibernate=INFO # Log JDBC parameters log4j.logger.org.hibernate.type=ALL Struts startup failed due to logging...

Java Class loading

java,class,classloader
I want to understand how Java class loaders works. I have read some articles, but there are some things are still unclear to me. As far as I understand the first class loader is the Bootstrap class loader (BCL). Is it loaded by the JVM? Afterwards, the BCL loads rt.jar...

Fixing compiled version

java,classloader
I had a look, and could not find any answers specific to my question, so I am making a new one. So I am currently searching for a solution to an issue which is formally out of my control, but would be great if there was something I could attempt...

The usage of `getClass` method in Scala/Java?

java,scala,oop,object,classloader
I find this works well (Scala codes) class myClass(i:Int) { } val a = new myClass(1) But this doesn't work: val b = a.getClass val c = new b(1) The compiler will say cannot resolve "b". Does anyone have ideas about this? What are the difference between b and myClass...

symfony 2.5 use ClassLoader to add Zend_pdf

symfony2,classloader,zend-pdf
I'm working in Symfony 2.5 and want to use the zend framework for pdf (zend_pdf) I tried the classloader but I probably do something wrong because it doesn't work (of course)... I don't know where to add my classloader... I thought in autoload.php but I would not know how... this...