Define custom system classloader - java

I'm trying to tell the JVM to use my custom ClassLoader as default ClassLoader
This is the VM argument i use to pick my class:
-Djava.system.class.loader=JarClassLoader
and this is the error i get
Error occurred during initialization of VM
java.lang.Error: java.lang.NoSuchMethodException: JarClassLoader.<init>(java.lang.ClassLoader)
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
Caused by: java.lang.NoSuchMethodException: JarClassLoader.<init>(java.lang.ClassLoader)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
Do i have to define a specific method or am i using the wrong argument?

Custom ClassLoader
public class CustomClassLoader extends ClassLoader{
public CustomClassLoader(ClassLoader classLoader) {
super(classLoader);
}
#Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
System.out.println("Loading class :" + name);
return super.loadClass(name);
}
}
Main Class
public class Main {
public static void main(String[] args) {
System.out.println("Starting main");
}
}
VM arguments: -Djava.system.class.loader=CustomClassLoader
All classes are in default package and it executes successfully.
Output:
Loading class :Main
Starting main

Related

Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class View

I am trying to instantiate an object of type Application outside of where I wrote my static main method and I am getting this exception.
public class Main {
public static void main(String[] args) {
new View(args);
}
}
import javafx.application.Application;
public class View extends Application {
public View(String... args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
}
}
The stack trace:
Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class View
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$2/1867083167.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$158(LauncherImpl.java:819)
at com.sun.javafx.application.LauncherImpl$$Lambda$46/1861073381.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1540794519.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$50/1604144171.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/718368050.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/1823101961.run(Unknown Source)
... 1 more
Caused by: java.lang.IllegalStateException: Application launch must not be called more than once
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:162)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:143)
at javafx.application.Application.launch(Application.java:252)
at View.<init>(View.java:33)
... 18 more
Not sure what are you trying to do, but invoking launch(args) in your constructor looks wrong. From javadocs (https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html#launch-java.lang.String...-):
The launch method does not return until the application has exited, either via a call to Platform.exit or all of the application windows have been closed.
Even if it worked, it would hang in your constructor. If you need to do it outside main() method, use some static instantiator.

Java Reflections 'NoClassDef' error

I'm trying to browse all classes that have implemented an interface using the custom libary Reflections. Here is my source :
public static List<IModdable> getAllModClasses() {
Reflections reflections = new Reflections("mod.api.core"); //getting error here
Set<Class<? extends IModdable>> classes = reflections.getSubTypesOf(IModdable.class);
List<IModdable> modList = new ArrayList<IModdable>();
for (Class<? extends IModdable> c : classes)
try {
modList.add((IModdable) c.newInstance());
} catch (Exception ex) {
err(String.format("Could not load mod %s !", c.getName()));
}
return modList;
}
error:
Exception in thread "Client thread" java.lang.NoClassDefFoundError: javassist/bytecode/ClassFile
at org.reflections.adapters.JavassistAdapter.getOfCreateClassObject(JavassistAdapter.java:100)
at org.reflections.adapters.JavassistAdapter.getOfCreateClassObject(JavassistAdapter.java:24)
at org.reflections.scanners.AbstractScanner.scan(AbstractScanner.java:30)
at org.reflections.Reflections.scan(Reflections.java:238)
at org.reflections.Reflections.scan(Reflections.java:204)
at org.reflections.Reflections.<init>(Reflections.java:129)
at org.reflections.Reflections.<init>(Reflections.java:170)
at org.reflections.Reflections.<init>(Reflections.java:143)
at mod.api.core.CoreProvider.getAllModClasses(CoreProvider.java:17)
at mod.api.core.ModCore.onLoad(ModCore.java:13)
at net.minecraft.client.Minecraft.run(Minecraft.java:405)
at net.minecraft.client.main.Main.main(Main.java:114)
at Start.main(Start.java:11)
Caused by: java.lang.ClassNotFoundException: javassist.bytecode.ClassFile
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
The mod.api.core package exits. So that shouldn't be the error.
java.lang.NoClassDefFoundError: javassist/bytecode/ClassFile
You can fix the issue by adding javassist-3.12.1.GA.jar to your classpath

AbstractUIPlugin causing NoClassDefFoundError

I am writing an Eclipse plugin that supports automated testing like JUnit. I have a Launch Configuration Delegate that launches my Main class, which will run the tests on the class the user provides.
I want to programmatically show a custom View from my Eclipse plugin when I launch it with my Launch Configuration Delegate. I keep getting a NoClassDefFoundError for the AbstractUIPlugin class when I try to launch it even though I have included both org.eclipse.ui and org.eclipse.core.runtime in my plugin dependencies.
Stack Trace
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/ui/plugin/AbstractUIPlugin
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at eoo.thefamilycoder.specj.internal.launching.delegate.SpecJMain.main(SpecJMain.java:8)
Caused by: java.lang.ClassNotFoundException: org.eclipse.ui.plugin.AbstractUIPlugin
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: SpecJ
Bundle-SymbolicName: eoo.thefamilycoder.specj;singleton:=true
Bundle-Version: 1.0.0.alpha
Bundle-Activator: eoo.thefamilycoder.specj.internal.Activator
Require-Bundle: org.eclipse.ui;bundle-version="3.105.0",
org.eclipse.core.runtime;bundle-version="3.9.0",
org.eclipse.jdt.launching;bundle-version="3.7.0",
org.eclipse.debug.core;bundle-version="3.8.0",
org.eclipse.jdt.core;bundle-version="3.9.1",
org.eclipse.debug.ui;bundle-version="3.9.0",
org.eclipse.jdt.ui;bundle-version="3.9.1",
org.eclipse.jdt.debug.ui;bundle-version="3.6.200"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Main class
import eoo.thefamilycoder.specj.internal.Activator;
public class SpecJMain {
public static void main(String[] args) {
Activator.getDefault().showView();
}
}
What is causing the error, and how can I fix it?
Edit: Adding launch code
Launch Configuration Delegate
public class SpecJLaunchConfigurationDelegate extends AbstractJavaLaunchConfigurationDelegate {
private static final int UNITS_OF_WORK = 4;
#Override
public void launch(final ILaunchConfiguration configuration, final String mode, final ILaunch launch,
final IProgressMonitor monitor) throws CoreException {
final IProgressMonitor pm = monitor != null ? monitor : new NullProgressMonitor();
pm.beginTask(configuration.getName(), UNITS_OF_WORK);
if (pm.isCanceled()) return;
try {
preLaunchCheck(configuration, mode, pm);
if (pm.isCanceled()) return;
final VMRunnerConfiguration runConfig =
new VMRunnerConfigurationFactory().create(configuration, monitor, this);
if (pm.isCanceled()) return;
getVMRunner(configuration, mode).run(runConfig, launch, pm);
if (pm.isCanceled()) return;
} finally {
pm.done();
}
}
#Override
public String[] getClasspath(ILaunchConfiguration configuration) throws CoreException {
final String[] initialClasspath = super.getClasspath(configuration);
final String pluginClasspath = new SpecJClasspathLocator().getClasspath();
System.out.println(pluginClasspath);
if (pluginClasspath == null) return initialClasspath;
final String[] classpath = new String[initialClasspath.length + 1];
classpath[0] = pluginClasspath;
System.arraycopy(initialClasspath, 0, classpath, 1, initialClasspath.length);
return classpath;
}
}
VMRunnerConfigurationFactory
public class VMRunnerConfigurationFactory {
public VMRunnerConfiguration create(final ILaunchConfiguration configuration, final IProgressMonitor monitor,
final AbstractJavaLaunchConfigurationDelegate delegate) throws CoreException {
final ExecutionArguments execArgs = new ExecutionArguments(delegate.getVMArguments(configuration), "");
// Activator.MAIN_TYPE_NAME is the main class above
final VMRunnerConfiguration runConfig = new VMRunnerConfiguration(Activator.MAIN_TYPE_NAME,
delegate.getClasspath(configuration));
runConfig.setBootClassPath(delegate.getBootpath(configuration));
runConfig.setEnvironment(delegate.getEnvironment(configuration));
runConfig.setProgramArguments(new ProgramArgumentsFactory().create(configuration, monitor));
runConfig.setVMArguments(execArgs.getVMArgumentsArray());
runConfig.setVMSpecificAttributesMap(delegate.getVMSpecificAttributesMap(configuration));
runConfig.setWorkingDirectory(getWorkingDirectoryName(delegate.verifyWorkingDirectory(configuration)));
return runConfig;
}
private String getWorkingDirectoryName(final File workingDir) throws CoreException {
return workingDir != null ? workingDir.getAbsolutePath() : null;
}
}
AbstractUIPlugin is in the org.eclipse.ui.workbench plug-in so you need that in your dependencies.
Edit.
Just noticed you are using a main method - this an Eclipse plugin you can't run this is as a normal Java program. You must either run it in an existing Eclipse RCP or create a RCP.
Edit 2
Your launch does nothing to properly initialize Eclipse to run a plugin. You must start an Eclipse app through the org.eclipse.equinox.launcher.Main entry point (normally via the eclipse executable). You must specify an application or product to run (like in the Run Configuration for an Eclipse Application).

Eclipse cannot find the main class in JFace?

I'm working though a book SWT/JFace IN ACTION by Manning Press.
When I added JFace, Eclipse for some reason could not find the main class though it is plainly present.
Here is the code
package com.swtjface.ChTwo;
import org.eclipse.jface.window.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class HelloSWT_JFace extends ApplicationWindow{
/**
* #param args
*/
public HelloSWT_JFace(){
super(null);
}
protected Control createContents(Composite parent){
Text helloText = new Text(parent, SWT.CENTER);
helloText.setText("Hello SWT and JFace!");
parent.pack();
return parent;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloSWT_JFace awin = new HelloSWT_JFace();
awin.setBlockOnOpen(true);
awin.open();
Display.getCurrent().dispose();
}
}
The reject message I get from Eclipse is...
Could not find the main class: com.swtjface.ChTwo.HelloSWT_JFace.
Program will exit.
Here is the exception...
java.lang.NoClassDefFoundError: org/eclipse/core/runtime/IProgressMonitor
Caused by: java.lang.ClassNotFoundException: org.eclipse.core.runtime.IProgressMonitor
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Exception in thread "main"
You need at least 2 more jars to use JFace:
org.eclipse.equinox.common
org.eclipse.core.commands
See Using JFace outside the Eclipse platform for more details.
You need to add jar file for "org.eclipse.core.runtime.IProgressMonitor class"
Check this link.

Bluetooth does not run in window PC

hi i write a java app about bluetooth
when i run in eclipse console it ran ok
but when i run in command line it occur error
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.LocalDevice;
public class main {
/**
* #param args
* #throws BluetoothStateException
*/
public static void main(String[] args) throws BluetoothStateException {
// TODO Auto-generated method stub
LocalDevice local=null;
local = LocalDevice.getLocalDevice();
System.out.printf("this is some text");
}
}
errors occur
C:\Users\HUNGSON\workspace\Java_Test\bin>java main
Exception in thread "main" java.lang.NoClassDefFoundError: main
Caused by: java.lang.ClassNotFoundException: main
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: main. Program will exit.
please help me !!

Categories

Resources