I currently try to run java code in Maxmsp using a mxj object, and I want to load some classes inside of the code.
But I always get the errors, although the code runs properly in eclipse.
What is the problem?
This is my code.
If I bang in Maxmsp, call() will be called.
package Load;
import com.cycling74.max.*;
public class Loaded extends MaxObject{
public static void main(String[] args) {
//This works properly in eclipse
call();
}
public void bang() {
//This should work in Maxmsp, but get errors
call();
}
public static void call() {
try {
//this is just a example
//I want to load some classes which locate the same directory as this class
Thread.currentThread().getContextClassLoader().loadClass("Load.Loaded");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
And this is the error message:
java.lang.ClassNotFoundException: Load.Loaded
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Load.Loaded.call(Loaded.java:21)
at Load.Loaded.bang(Loaded.java:16)
MXJ System class path is:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/commons-codec-1.11.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/core.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/gluegen-rt-natives-macosx-universal.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/gluegen-rt.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/jitter.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/jode-1.1.2-pre-embedded.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/jogl-all-natives-macosx-universal.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/jogl-all.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/max.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/sadamLib.jar
MXJ Classloader CLASSPATH is:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/classes/
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/
/Users/MyName/Documents/ecllipse-workspace/009_Processing/bin
Loaded.class is in /Users/MyName/Documents/ecllipse-workspace/009_Processing/bin
You need to include any of your dependencies on the classpath:
java -cp "path/to/maxmsp.jar;path/to/dependency2.jar;path/to/your.jar" classpath.of.your.Main
If you are just running directly from a classfile and haven't JARred your project then you can omit the path/to/your.jar and just run from the same directory with the classpath of your Main.
The above is for running java from command line.
Since Max is what is running and what's taking control of the classloading im guessing that sun.misc.Launcher$AppClassLoader is not working. Try debugging and see what it's doing. Also maybe try to find a way to use the Max classloader instead of the Java AppClassLoader.
The problem was what Max cannot load class properly.
So I created class loader method.
public static ClassLoader createClassLoader(String dirname) throws java.io.IOException {
java.net.URL[] url = new java.net.URL[1];
java.io.File file;
if (dirname.endsWith("/")) {
file = new java.io.File(dirname);
}
else {
file = new java.io.File(dirname + "/");
}
url[0]= file.toURI().toURL();
ClassLoader parent = ClassLoader.getSystemClassLoader();
java.net.URLClassLoader loader = new java.net.URLClassLoader(url, parent);
return loader
}
And call
ClassLoader loader = createClassLoader("ClassPath");
Class<?> c = Class.forName("Classname", true, loader);
The following code adds jar file to the build path, it works fine with Java 8. However, it throws exception with Java 9, the exception is related to the cast to URLClassLoader. Any ideas how this can be solved? an optimal solution will edit it to work with both Java 8 & 9.
private static int AddtoBuildPath(File f) {
try {
URI u = f.toURI();
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class<URLClassLoader> urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(urlClassLoader, u.toURL());
} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException | MalformedURLException | IllegalAccessException ex) {
return 1;
}
return 0;
}
You've run into the fact that the system class loader is no longer a URLClassLoader. As indicated by ClassLoader::getSystemClassLoader's return type, this was an implementation detail, albeit one that a non-negligible amount of code relied upon.
Judging by the comments, you are looking for a way to dynamically load classes at run time. As Alan Bateman points out, this can not be done in Java 9 by appending to the class path.
You should instead consider creating a new class loader for that. This has the added advantage that you'll be able to get rid of the new classes as they are not loaded into the application class loader. If you're compiling against Java 9, you should read up on layers - they give you a clean abstraction for loading an entirely new module graph.
I have stumbled over this issue a while ago. As many, I had used a method similar to that in the question
private static int AddtoBuildPath(File f)
to dynamically add paths to the classpath at runtime. The code in the question is probably bad style in multiple aspects: 1) assuming that ClassLoader.getSystemClassLoader() returns an URLClassLoader is an undocumented implementation detail and 2) using reflection to make addURL public is maybe another one.
Cleaner way to dynamically add classpaths
In case that you need to use the additional classpath URLs for class loading through „Class.forName“, a clean, elegant and compatible (Java 8 to 10) solution is the following:
1) Write your own class loader by extending URL classloader, having a public addURL method
public class MyClassloader extends URLClassLoader {
public MyClassloader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
public void addURL(URL url) {
super.addURL(url);
}
}
2) Declare a (singleton/app wide) object of your classloader
private final MyClassloader classLoader;
and instanciate it via
classLoader = new MyClassloader(new URL[0], this.getClass().getClassLoader());
Note: The system class loader is the parent. Classes loaded though classLoader know those who can be loaded through this.getClass().getClassLoader() but not the other way around.
3) Add additional classpaths whenever needed (dynamically):
File file = new File(path);
if(file.exists()) {
URL url = file.toURI().toURL();
classLoader.addURL(url);
}
4) Instanciate objects or your app though your singleton classloader via
cls = Class.forName(name, true, classLoader);
Note: Since class loaders try a delegation to the parent class loader prior loading a class (and the parent to its parent), you have to make sure that the class to load is not visible to the parent class loader to make sure that it is loaded through the given class loader. To make this clearer: if you have ClassPathB on your system class path and later add ClassPathB and some ClassPathA to your custom classLoader, then classes under ClassPathB will be loaded through the system classloader and classes under ClassPathA are not known to them. However, if you remove ClassPathB from you system class path, such classes will be loaded through your custom classLoader, and then classes under ClassPathA are known to those under ClassPathB.
5) You may consider passing your class loader to a thread via
setContextClassLoader(classLoader)
in case that thread uses getContextClassLoader.
If you're just looking to read the current classpath, for example because you want to spin up another JVM with the same classpath as the current one, you can do the following:
object ClassloaderHelper {
def getURLs(classloader: ClassLoader) = {
// jdk9+ need to use reflection
val clazz = classloader.getClass
val field = clazz.getDeclaredField("ucp")
field.setAccessible(true)
val value = field.get(classloader)
value.asInstanceOf[URLClassPath].getURLs
}
}
val classpath =
(
// jdk8
// ClassLoader.getSystemClassLoader.asInstanceOf[URLClassLoader].getURLs ++
// getClass.getClassLoader.asInstanceOf[URLClassLoader].getURLs
// jdk9+
ClassloaderHelper.getURLs(ClassLoader.getSystemClassLoader) ++
ClassloaderHelper.getURLs(getClass.getClassLoader)
)
By default the final fields in the $AppClassLoader class cannot be accesed via reflection, an extra flag needs to be passed to the JVM:
--add-opens java.base/jdk.internal.loader=ALL-UNNAMED
I was given a spring boot application that runs in Java 8. I had the task to upgrade it to Java 11 version.
Issue faced:
Caused by: java.lang.ClassCastException: jdk.internal.loader.ClassLoaders$AppClassLoader (in module: java.base) cannot be cast to java.net.URLClassLoader (in module: java.base)
Way around used:
Create a class:
import java.net.URL;
/**
* This class has been created to make the code compatible after migration to Java 11
* From the JDK 9 release notes: "The application class loader is no longer an instance of
* java.net.URLClassLoader (an implementation detail that was never specified in previous releases).
* Code that assumes that ClassLoader.getSytemClassLoader() returns a URLClassLoader object will
* need to be updated. Note that Java SE and the JDK do not provide an API for applications or
* libraries to dynamically augment the class path at run-time."
*/
public class ClassLoaderConfig {
private final MockClassLoader classLoader;
ClassLoaderConfig() {
this.classLoader = new MockClassLoader(new URL[0], this.getClass().getClassLoader());
}
public MockClassLoader getClassLoader() {
return this.classLoader;
}
}
Create Another class:
import java.net.URL;
import java.net.URLClassLoader;
public class MockClassLoader extends URLClassLoader {
public MockClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
public void addURL(URL url) {
super.addURL(url);
}
}
Now set it in the current thread from your main class (Right at the beginning of your application)
Thread.currentThread().setContextClassLoader(new ClassLoaderConfig().getClassLoader());
Hope this solution works for your!!!
Shadov pointed to a thread at the oracle community. There is the correct answer:
Class.forName("nameofclass", true, new URLClassLoader(urlarrayofextrajarsordirs));
The caveats mentioned there are also important:
Caveats:
java.util.ServiceLoader uses the thread's ClassLoader context Thread.currentThread().setContextClassLoader(specialloader);
java.sql.DriverManager does honors the calling class' ClassLoader, -not- the Thread's ClassLoader. Create Driver directly using Class.forName("drivername", true, new URLClassLoader(urlarrayofextrajarsordirs).newInstance();
javax.activation uses the thread's ClassLoader context (important for javax.mail).
Referring to Edi's Solution this worked for me:
public final class IndependentClassLoader extends URLClassLoader {
private static final ClassLoader INSTANCE = new IndependentClassLoader();
/**
* #return instance
*/
public static ClassLoader getInstance() {
return INSTANCE;
}
private IndependentClassLoader() {
super(getAppClassLoaderUrls(), null);
}
private static URL[] getAppClassLoaderUrls() {
return getURLs(IndependentClassLoader.class.getClassLoader());
}
private static URL[] getURLs(ClassLoader classLoader) {
Class<?> clazz = classLoader.getClass();
try {
Field field = null;
field = clazz.getDeclaredField("ucp");
field.setAccessible(true);
Object urlClassPath = field.get(classLoader);
Method method = urlClassPath.getClass().getDeclaredMethod("getURLs", new Class[] {});
method.setAccessible(true);
URL[] urls = (URL[]) method.invoke(urlClassPath, new Object[] {});
return urls;
} catch (Exception e) {
throw new NestableRuntimeException(e);
}
}
}
Running within Eclipse, you need to set VM Arguments to JUnit Launch/Debug Configuration.
Running with maven via command line you have two options:
Option 1
Add following lines to pom.xml :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<argLine>--add-opens java.base/jdk.internal.loader=ALL-UNNAMED</argLine>
</configuration>
</plugin>
Option 2
run mvn test -DargLine="-Dsystem.test.property=--add-opens java.base/jdk.internal.loader=ALL-UNNAMED"
There's also this guys article that helped me.
I could not find the article but... here: https://github.com/CGJennings/jar-loader
Here's a part of guide inside there there's a jar at release you could read his guide & setup it up.
I just tried it myself download the jar file which include the class file
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarFile;
public final class classname{
public static void premain(String agentArgs, Instrumentation instrumentation) {
loadedViaPreMain = true;
agentmain(agentArgs,instrumentation);
}
public final static void addToClassPath(File jarfile)throws IOException{inst.appendToSystemClassLoaderSearch(new JarFile(jarfile));}
public final static void agentmain(String agentArgs, Instrumentation instrumentation) {
if (instrumentation == null){throw new NullPointerException("instrumentation");}
if (inst == null) {inst = instrumentation;}
}
private static Instrumentation inst;
private static boolean loadedViaPreMain = false;
}
I just try it out myself package these code as a package then start the application class with -javaagent:plugin......jar option then call this function.It doesn't change my classpath.I am probably missing some details here.
Hope you can make it work though.
i found this, and worked for me.
String pathSeparator = Syste .getProperty("path.separator");
String[] classPathEntries = System.getProperty("java.class.path") .split(pathSeparator);
from the web site https://blog.codefx.org/java/java-11-migration-guide/#Casting-To-URL-Class-Loader
I'm currently developing on OS X and trying to load the librxtxSerial.jnilib with System.load(), which just doesn't work and always results in
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
When I place the lib in /Library/Java/Extensions everything works fine.
I have double checked paths and everything, but it just won't work with System.load when I remove the lib from /Library/Java/Extensions.
I want to bundle the jnilib with a distributable jar, that's why I want to load it programmatically.
Does anybody have an idea?
if you have this:
root:
main.jar
libjni.jnilib
And you run your code at the root directory, using command like:
java -jar main.jar
In this case, your loading code should be like:
System.load("libjni.jnilib");
But bot System.loadLibrary(), load is safer than loadLibrary.
It's recommended to pass the absolute path of your jni library to System.load.
I used something like this in my project:
/**
* To load the JNI library
* Created by ice1000 on 2017/1/6.
*
* #author ice1000
*/
#SuppressWarnings("WeakerAccess")
public final class Loader {
public final static String JNI_LIB_NAME;
private static boolean loaded = false;
/*
* maybe it's already loaded, so there should be a check
*/
static {
JNI_LIB_NAME = "libjni";
loadJni();
}
#NotNull
#Contract(pure = true)
private static String libraryName(#NonNls #NotNull String libName) {
String ___ = System.getProperty("os.name");
String fileName;
if (___.contains("Linux"))
fileName = libName + ".so";
else if (___.contains("Windows"))
fileName = libName + ".dll";
else // if (___.get("OSX"))
fileName = libName + ".dylib";
// else fileName = libName;
return new File(fileName).getAbsolutePath();
}
public static void loadJni() {
if (!loaded) {
System.load(libraryName(JNI_LIB_NAME));
loaded = true;
}
}
}
here's my working directory:
root:
javaClasses.jar
libjni.dll
libjni.so
libjni.dylib
Hope this can help you.
Make sure to put your library on LD_LIBRARY_PATH.
Take a look here for lots of JNI related samples written for macOS/Linux.
http://jnicookbook.owsiak.org
I suggest to start with supper simple Hello world app:
http://jnicookbook.owsiak.org/recipe-No-001/
You can take a look there how to develop with JNI for macOS using standard tools.
Hope this helps. Have fun with JNI.
Hello I am looking to run a groovy script inside Java code but I didn't find many tutorial about that.
I have a String that contain a groovy script :
private String processingCode = "def hello_world() { println \"Hello, world!\" }";
I have also downloaded the Groovy SDK.
Which groovy jar should I include in java project ? And how to execute the script in Java ?
What you need is a groovy-all dependency and GroovyShell.
Main class will be:
package lol;
import groovy.lang.GroovyShell;
public class Lol {
public static void main(String[] args) {
String processingCode = "def hello_world() { println 'Hello, world!' }; hello_world();";
GroovyShell shell = new GroovyShell();
shell.evaluate(processingCode);
}
}
Here is a demo.
Use gradle run to run it.
I am developing a project in java in which ,after running main file , some java files get altered and if i run that file again during the same execution the output does not show the changes done in the java file
For example there are 2 files. Main.java and file1.java
main.java
public static void main(string[] argv)
{
file1 obj = new file1();
obj.view();
Scanner in = new Scanner(System.in);
String x = in.nextLine();
//before entering any value i manually updated the content of file1.java
obj = new file1();
obj.view();
}
file1.java (Before updation)
public class file1
{
public void view()
{
system.out.println("This is test code!!");
}
}
file1.java (After updation)
public class file1
{
public void view()
{
system.out.println("That was done for Testing!!");
}
}
Output :
This is test code!!
This is test code!!
You have to recompile the code in order to see the changes.
What you can do is compile a string (after reading it from the file) with java and call methods of classes through reflection.
HERE is a step by step guide on how to compile a string programatically.
Updating a Java file will not impact the runtime instructions executed by the JVM.
When the compile a Java application the .java source code files are compiled into .class files containing byte code instructions which are in turn interpreted by the JVM. When the JVM requires a class it loads the appropriate .class file into memory via a special object known as a classloader.
Applying this to your example - when you first reference the class File1 the JVM will load the File1 class into memory. This in memory representation of the class will persist until either the classloader is destroyed or the JVM is restarted. No change to the file1.java class is visible to the JVM - firstly because the classloader wont reload the definition and secondly because the definition wont change until the file1.java class is recompiled.
To change an object's behaviour at runtime you can use the reflection API see here.
You don't need to compile source code to accomplish anything close to your example.
public class MyView
{
String the_string;
public MyView (String string) { the_string = string; }
public void setString (String string) { the_string = string; }
public void view () { system.out.println (the_string); }
}
public static void main(string[] argv)
{
MyView obj = new MyView("This is test code!!");
obj.view();
Scanner in = new Scanner(System.in);
obj.setString (in.nextLine());
obj.view();
}