2

I am trying to load a Java file to my android project using URLClassLoader class.

But I got a ClassNotFoundError when I run the below code:

package com.xyz.abc;
public class ConstantClassReader
{
    public void readFile() throws MalformedURLException
    {
        String url = "file://"+Environment.getExternalStorageDirectory().getPath()+"/StringConstants.class";
        URLClassLoader urlClassLoader = URLClassLoader.newInstance(new URL[]{new URL(url)});
        Class simpleClass = null;
        try {
            simpleClass = urlClassLoader.loadClass("com.xyz.abc.StringConstants");
            Constructor simpleConstructor = simpleClass.getConstructor();
            Object simpleClassObj = simpleConstructor.newInstance();
            Method method = simpleClass.getMethod("myMethod");
            method.invoke(simpleClassObj);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

The below Java file is resides inside my SDCard:

package com.xyz.abc;
public class StringConstants{
    public void myMethod(){
        System.out.println("myMethod Loaded");
    }
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    In case you write another question: *clearly* outline your requirement; and maybe give some example code how your app will make *use* of those many entries that come from somewhere dynamically. And drop me a comment; I will have a look then. – GhostCat May 30 '17 at 07:21
  • https://stackoverflow.com/questions/44242745/how-to-change-language-in-android-based-on-web-service –  May 30 '17 at 09:28

2 Answers2

2

A class loader loads classes.

Putting a Java source file somewhere will not do.

You have to compile that file; and maybe you are lucky then. But I would be rather surprised if the Android JVM allows you to load classes from arbitrary places. This screams: "security problem" all over the place.

Given your requirement, I would suggest a different solution, something on top of ordinary Java properties. Meaning: it seems that you simply want to provide some "configuration" information dynamically to your Java app. Then have that app read a properties file that contains key/value pairs. That is (almost) business as usual; and not leading to a need to load class files in arbitrary places.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Is there any possibility to load JAR files from arbitrary places –  May 30 '17 at 06:50
  • Please note: I am not saying that this is impossible. I would just find it a bit strange. And in that sense, it doesn't make (much) of a difference between JAR and class files; as a JAR is nothing but an archive of classes in the end. Maybe you should rather step back and explain to us *why* you think you have to do things this way. – GhostCat May 30 '17 at 06:52
  • In that `StringConstants` java file, I'm going to store many static string fields. This java file will be generated from the server and downloaded automatically using Android application.After that I'll access this static strings across my android application –  May 30 '17 at 06:55
  • I'm also had an idea to store all the data in a HashMap. Is this better way? –  May 30 '17 at 07:04
  • The point is: this is *dynamic* information. One straight forward way is to put that into a file that matches the java properties specification, and simply use that support to read it. Then you basically end up with a map with key/values ... – GhostCat May 30 '17 at 07:07
  • The problem is I'm trying to access a dataset with more than 1 lakh entries. Using HashMap or Key-Value pair may slow down the application. So, I thought to load static final constants will be much faster. Is this correct idea? –  May 30 '17 at 07:10
1

http://www.beanshell.org/

you can embed beanshell, load the java file, instance and execute whatever.

access the java file as any other plain file from the sd

How can I read a text file from the SD card in Android?

eduyayo
  • 2,020
  • 2
  • 15
  • 35
  • https://stackoverflow.com/questions/44242745/how-to-change-language-in-android-based-on-web-service This is my actual problem –  May 30 '17 at 09:29