classloader

Will new instance of JVM or reflection help in this case

偶尔善良 提交于 2019-12-01 08:43:20
I had a problem that I posted before but got no clear solution How to prevent JFrame from closing . So I am posting a SSCCE may be this might help in better understanding the problem being faced package myApp; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import javax.swing.JFrame; import App2.Applic2; public class MYApp { @SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String arg[]){ JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setTitle("Application frame 1"); f.setSize(200,200); f.setVisible(true);

Loading resource bundles using a custom class loader

北战南征 提交于 2019-12-01 08:31:16
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class JarClassLoader extends ClassLoader { private String path; public JarClassLoader(String path) { this.path = path; } @Override public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class<?> c = findLoadedClass(name); if (c == null) { try { c = findSystemClass(name); } catch (Exception e) { } if (c != null) return c; try { byte data[] = loadClassData(name); c = defineClass(name

Spring ApplicationContext with multiple XML Files from Jar

亡梦爱人 提交于 2019-12-01 08:20:39
问题 I need to create a ApplicationContext with the "main" applicationContext-a.xml from the current Maven build. The other one wires classes from another maven build and is preset in the jar included by a Maven Dependency. Here the idea: ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath*:applicationContext-*.xml"}); This should load applicationContext-a.xml from the Classpath because it's in the same Project. This works. Then applicationContext-b.xml

How to get a resource in another jar

余生长醉 提交于 2019-12-01 08:09:23
I have a jar embedded in a bundle that needs to fetch a resource packaged with it like so: MyBundle -\ src -\lib -\MyEmbeddedJar -\src -\SomeClass -\someResource.xml I am trying to access 'someResource.xml' from 'SomeClass' like so: SomeClass.class.getResource( "someResource.xml" ); But I've had no luck. I've tried several variations with the CWD appended (eg: './someResource.xml') but I just can't get this resource to load. I know that the "right" way is to use Activator to get hooks back to the proper classloader, but the embedded jar can be used in other projects, so I'd hate to have to add

Java casting / classloader issues

ⅰ亾dé卋堺 提交于 2019-12-01 08:04:54
Here is the simplified version of the problem: SomeClass c = (SomeClass) obj.getSomeClassParent() not always but it happens sometimes to trigger exception org.somepackage.SomeClass can't be cast to org.somepackage.SomeClass How is this possible ? I suppose it has something to do with the fact that JAI imageio is native lib, but relay how can this happen ? I'm probably missing something but what ? I'm using JAI imageio version 1.1 dcm4che 2.0.21 DICOM lib Here is the original code ImageInputStream iis = ImageIO.createImageInputStream(src); Iterator<ImageReader> iter = ImageIO

Applet class loader cannot find a class in the applet's jar

蓝咒 提交于 2019-12-01 07:38:39
I started to ask this question and then figured out the answer before submitting it. I've decided to post the question anyway so that other people who run into the same problem will be able to learn from my mistakes. I'm having a problem with an applet (a JApplet actually) unable to instantiate another class which is included in the same jar as the applet. The exception I'm seeing on the Java console is: Exception in thread "thread applet-com.company.program.cm.hmi.MediatorApplet-1" java.lang.NoClassDefFoundError: com/company/program/cm/cs/JDataStore at com.company.program.cm.hmi

Replace implementation of instantiated class without touching the code (java)

微笑、不失礼 提交于 2019-12-01 07:35:45
I have legacy code I don't want to touch. public class LegacyCode{ public LegacyCode() { Service s = new ClassA(); s.getMessage(); } } Where ClassA provides a CORBA service call. public class ClassA implements Service{ @Override public String getMessage() { // Make CORBA service call... return "Class A"; } } And the interface Service looks like; public interface Service { String getMessage(); } For test purposes, I want to replace the implementation of Service (in LegacyCode implemented by ClassA ) with a stub. public class ClassB implements Service { @Override public String getMessage() {

How JVM loads parent classes in Java

杀马特。学长 韩版系。学妹 提交于 2019-12-01 07:33:49
Code: class A { static { System.out.println("loading A static 1"); } static { System.out.println("loading A static 2 B.c= "+B.c); } static { System.out.println("loading static 3"); } static int a=10; A(){ } } class B extends A{ static { System.out.println("loading B A.a= "+A.a); } static int c = 50; } public class Test { public static void main(String[] args) { new B(); } } Output: loading A static 1 loading A static 2 B.c= 0 loading static 3 loading B A.a= 10 From this out put can we say that the parent class loads after the child class but child class initialize after the parent class? If so

this.getClass().getClassLoader().getResourceAsStream always returning null

拟墨画扇 提交于 2019-12-01 07:28:27
问题 While reading a file in a web application with this.getClass().getClassLoader().getResourceAsStream(../abc.txt) is always returning null. Can anyone please provide details where should I put abc.txt in order to read. And any article on getResourceAsStream will be helpful. I have searched a lot but did not get any relevant information. Please provide your valuable suggestions. Thanks. 回答1: If your abc.txt is in classpath and in different package like com/test/oops/testpaper/abc.txt . Then read

How can I locate a non-Java resource in a WAR at runtime?

自古美人都是妖i 提交于 2019-12-01 07:06:36
问题 I need to run a shell script at runtime from a WAR that I run on Tomcat. Thus, I've placed my script theScript.sh in my src/main/resources directory (because, yes, I use Maven). This directory is in the classpath, I've checked it. In the code, I want to copy my script in a temp directory. So I've tried to get it via my ClassLoader: URL myURL = ClassLoader.getSystemResource("theScript.sh"); if (myURL == null) { LOG.error("Couldn't find the resource"); } And guess what? Yes, the "Couldn't find