Using the Bukkit-API I'm currently trying to create a plugin which can compile java code of a given pastebin link at runtime and execute that code. Everything is working so far except for one thing: I'm getting a ClassNotFoundException when I'm trying to access one of the already loaded plugins. (I'm not getting an exception when I'm using Bukkit-API methods!)
All plugin jars have been added to the classpath; it currently looks like this:
/home/cubepanel/test-network/jars/craftcubbit.jar:/home/cubepanel/test-network/servers/ProHub/plugins/MultiCubeHub.jar:/home/cubepanel/test-network/servers/ProHub/plugins/MultiCubeCore.jar:
The classes I tried to load dynamically:
ClassNotFoundException for MutliCube
import be.multicu.core.MultiCube;
public class Test{
public void debug() {
System.out.println(MultiCube.getInstance());
}
}
Working
import org.bukkit.Bukkit;
public class Test{
public void debug() {
System.out.println(Bukkit.getClass().getName());
}
}
Sourcecode of RuntimeCompiler.java: http://paste.multicu.be/axigofekip.avrasm (It's a bit long, thats why I used a pastebin link)
I also noticed that I'm getting an compilation error when I removed the classpath of MultiCube.jar which means that the classpath must be correct since the sourcecode can be compiled.
EDIT: I was able to fix it by adding MultiCube.class.getClassLoader() as an argument in the constructor of my URLClassLoader
Related
I first met this problem in my Ideas, I wrote a Class that extends javax.servlet.http.HttpServlet, My Ideas throws an error message reads Error: Could not find or load main class com.bjpowernode.OneServlet, here is the image:
enter image description here
It can be seen that Idea didn't show the red wavy line, that shows my codes are fine. I found a solution to this problem from enter link description here, I changed the scope from provided to compile:
enter image description here
But I actually want to know why and how it works? I compared the difference of Idea compilation instructions under different scope setting,I found that when Idea uses java command to run the .Class, the parameter -classpath of the command of the compile scope has two more paths:
D:\apache-tomcat-8.0.50\lib\jsp-api.jar;D:\apache-tomcat-8.0.50\lib\servlet-api.jar
That's to say, Idea didn't consider external library paths when run .class under the provided scope, and the super Class HttpServlet is from servlet-api.jar package. Why?
To simplify the problem, I created two different classes under two different paths and packages: Class Base and Class Sub, and Sub extends Base.
The codes of the Base is here:
package base;
public class Base{
public static void main(String[] args) {
}
}
The codes of the Sub is here:
package sub;
import base.Base;
public class Sub extends Base{
public static void main(String[] args) {
System.out.println("In Sub");
}
}
The path of Base is ./path1/base/Base.java and the path of Sub is ./path2/sub/Sub.java.
I compiled them using these two commands:
javac ./path1/base/Base.java -d ./path1
javac ./path2/sub/Sub.java -d ./path2 -cp "./path1;./path2"
And compiled successfully. But when I run sub.Sub using the command below:
java sub.Sub -cp "./path1;./path2"
And I got the same error:
Error: Could not find or load main class sub.Sub
Ive tried multiple variations of this, but none of them seem to work. Any ideas? Although I solved the problem of idea reporting errors, I still could not understand the principle behind? I hope this question can help me to figure it out. My jdk version is 1.8. Thanks in advance.
The file "HelloDemo.java" path is "/test/hello/HelloDemo.java"
package test.hello;
public class HelloDemo {
public static void main(String[] args) {
System.out.print("Hello!!");
}
}
when I "run" it, an error occurred.
Building HelloDemo.java and running HelloDemo
Error: Could not find or load main class HelloDemo
Then, I changed the code.
//package test.hello;
public class HelloDemo {
public static void main(String[] args) {
System.out.print("Hello!!");
}
}
when I "Run" it, code success output.
Building HelloDemo.java and running HelloDemo
Hello!!
This is the screenshot about the "Run".
I fixed an error, but I don't konw why, I need help, Thank you!
If I want to keep the package uncomment, How to fix it?
That's because you probably changed the location of your file after running it once already. Hence, the running configuration should change to look for the new test.hello.HelloDemo class inside the built jar and not for HelloDemo anymore (which was probably in the default package, initially). What is your IDE?
Remark: This is not because you changed the location of your file that the classpath changed, and vice-versa.
On IntelliJ, you should do this: https://www.jetbrains.com/help/idea/creating-and-editing-run-debug-configurations.html
Create a package using your IDE and add your class to it. Package name will be appended to top automatically.
Reguardless of IDE, folder structure should match package structure, your problem could be here.
A class's name is actually the package plus the class name. You cannot run HelloDemo in your first case, because that is not the class name. The class name is test.hello.HelloDemo.
By commenting out the package, you've essentially renamed the class to HelloDemo, so it runs.
In addition, when running the class with main, you must be in the correct location. For instance, if the class is test.hello.HelloDemo, your folder structure will be /test/hello/HelloDemo.java.
You must be in / and run test.hello.HelloDemo from there.
Hi I had a program which worked fine in a .jar file.
Basically all the classes were part of the same package called : "eu.floraresearch.lablib.gui.checkboxtree"
They ALL are located in the SAME "checkboxTree" folder. Each file has a line stating
package eu.floraresearch.lablib.gui.checkboxtree;
I need to modify the code and to integrate it in another project.
So I took all the .java files, copied them in my Eclipse project folder (no package anymore), and got rid of the "package eu.floraresearch.lablib.gui.checkboxtree;" line in each files. Everything is fine except for one file comprising an enumeration.
Originally the file looked like this:
package eu.floraresearch.lablib.gui.checkboxtree;
public class QuadristateButtonModel extends DefaultButtonModel {
public enum State {
CHECKED, GREY_CHECKED, GREY_UNCHECKED, UNCHECKED
}
...
}
The problem is, there is another class which originally imported the above class enumeration:
import eu.floraresearch.lablib.gui.checkboxtree.QuadristateButtonModel.State;
public class QuadristateCheckbox extends JCheckBox {
public QuadristateCheckbox() {
this(null);
}
public QuadristateCheckbox(String text) {
this(text, State.UNCHECKED);
}
...
}
First of all I find it weird how enumerations can be imported.. But it worked fine when everything was inside the package.
Since all my .java files are in the same folder now, I just removed the package line.
However I have this issue with the QuadristateCheckbox class which imports "QuadristateButtonModel.State".
If I change the import line with
import QuadristateButtonModel.State;
it states
The import QuadristateButtonModel cannot be resolved
I tried various things I found on the internet like
import static QuadristateButtonModel.State.*;
or
import QuadristateButtonModel.State.*;
but the same error messages occur:
The import QuadristateButtonModel cannot be resolved
On top of that, in the above code from QuadristateCheckbox class:
public QuadristateCheckbox(String text) {
this(text, State.UNCHECKED);
}
an error message
State cannot be resolved to a variable
which is understandable given the fact that I fail to import the State enumeration.
What can I do? Please explain to me what is wrong
PS: The code was taken from this site: http://www.javaworld.com/article/2077762/core-java/swing-based-tree-layouts-with-checkboxtree.html
The authors provide classes to build checkable trees.
Some weird behavior in Netbeans 7.0. Ostensibly something went wrong when I created a class, because now no matter what project I am in, if I create a class named "RainbowBall" in a package called "gamesandbox.agents" (even if I just created the package fresh), it compiles fine, but the debugger gives me "Thread main stopped" when I call the RainbowBall constructor.
Stripped down example from a freshly created project:
//RainbowTest.java
package rainbowtest;
import gamesandbox.agents.RainbowBall;
public class RainbowTest
{
public static void main(String[] args)
{
RainbowBall r = new RainbowBall();
System.out.println(r.toString());
}
}
/*---------------*/
//RainbowBall.java
package gamesandbox.agents;
public class RainbowBall
{
public RainbowBall() {};
}
Again, this compiles fine, but the debugger acts like RainbowBall is an unresolvable symbol ("Thread Main Stopped at RainbowTest.java:10").
If I use any other class name (ex. "RainbowBall2") or any other package name I do not get this error. It happens in freshly created projects as well as old ones, and even when no outside libraries/jars/packages are being used in any way.
I'll probably just change the name or try updating to the latest NetBeans, but it would be good to understand what's going on. The IDE has clearly stored the name of the class somewhere permanent and project-agnostic, and is refusing to work with RainbowBalls like some kind of homophobe.
The output message you gave sounds like NetBeans thinks there is a breakpoint in the class. I'm not sure why it would be global to every project, though.
Recently when I write any code and compile it, then try to run it I get this exception:
Exception in thread "main" java.lang.NoSuchMethodError
At first I thought there is something wrong in my code, but I couldn't find anything wrong with it. When trying to run a HelloWorld example that had worked before, if works, but if I copy the exact same code into a file HelloWorld2 I get this exception again.
The code is identical but when I used javap to decompile both class files I found a difference. In HelloWorld (the original file)
"public static void main(java.lang.String[])";
and in HelloWorld2 (the new one)
"public static void main(String[])";
without java.lang..
I recompiled the old HelloWorld with javac and now when I try to run it it doesn't work and I get the same exception. None of my old code now works if I recompile it.
I've searched everywhere but can't find a solution to this problem - any idea what is going on here?
You may get this if you have your own class called String (without a package) in your classpath. It sounds like that's what happened. Here's a way to try to reproduce this - compile it and run it, and see if it looks the same:
class String {}
public class Test {
public static void main(String[] args) {
}
}
Once you've got the compiled String.class file in your file system in an awkward place, that will be used by default even if you only compile the Test class above...
Basically, see if you can find a file called String.class somewhere.