java.lang.ClassNotFoundException when instantiating an inner class - java

I have a following problem with an innner class. Here's the code:
public class PGZUserManagerBean {
// joomla login as separate thread
private class JoomlaLogin extends Thread {
private AuthJoomla authJoomla;
public JoomlaLogin(AuthJoomla authJoomla){
this.authJoomla = authJoomla;
}
#Override
public void run(){
this.authJoomla.authJoomla();
}
}
public void validateuser(){
AuthJoomla authJoomla = new AuthJoomla();
JoomlaLogin joomlaLogin = new JoomlaLogin(authJoomla);
joomlaLogin.start();
}
}
I'm getting java.lang.ClassNotFoundException: PGZUserManagerBean$JoomlaLogin on runtime. I'm using Java 1.6.
Thank you for the help in advance.
al

I strongly suspect that you've copied the class files from one place to another (or put them in a jar file) but you've failed to copy/include PGZUserManagerBean$JoomlaLogin.class.
Check where you're running the code, and look for the class file that the JVM can't find. It will definitely be in your compilation output.

Related

JNI Unsatisfied Link Error when calling the methods of one class but not the other

I want to wrap a C++ library (PCL) in Java code using JNI, but I am having inconsistent results. I have first created a PointXYZ class for testing and it looks like this:
package pcl;
public class PointXYZ extends NativeObject {
PointXYZ() { }
#Override
public native void alloc(); // creates pointer + handle on the native side
#Override
public native void dispose(); // sets handle to 0 and deletes pointer
public native float getX();
// ...
}
I have generated the C header for this class using javah, compiled everything using CMake, tested it using its getters and setters and everything works perfectly.
static {
System.setProperty("java.library.path", System.getProperty("user.dir") + "/lib");
System.loadLibrary("pcl_java_common");
}
#Test
void attributeAccessTest() {
PointXYZ p = new PointXYZ();
p.alloc();
p.setX(3);
assertEquals(p.getX(), 3);
p.dispose();
// all is good
}
Now I have done the exact same steps for a PointXYZRGB class which inherits from PointXYZ and when I try to test that it throws java.lang.UnsatisfiedLinkError. Here is the class:
package pcl;
public class PointXYZRGB extends PointXYZ {
public PointXYZRGB() { }
#Override
public native void alloc();
#Override
public native void dispose();
public native short getR();
// ...
}
I have checked the generated .dll using Dependency Walker and the PointXYZRGB methods are all present. Anyone knows what the problem could be?
UPDATE: Here are the .dll functions as requested in the comment:
The problem was that System.setProperty("java.library.path", System.getProperty("user.dir") + "/lib"); does not actually make Java look for .dll files in the given path. It essentially does nothing. Then why do the tests work for PointXYZ? This is was my mistake of having put an older .dll into the project root folder, so it was essentially looking for methods in that.

Bad class file when calling in another java file

When declaring a java file in another java file I get the following error.
The java file which is causing the error has the following code in
public class NumberSettingsFile
{
int maxSortBoxes = 50;
public int getMaxSortBoxes()
{
return maxSortBoxes;
}
}
And I am declaring it using the following code
NumberSettingsFile uniSet = new NumberSettingsFile();
Would someone please be able to explain what I am doing wrong?
The problem was caused by not declaring the package in which the java file was in. The code should have read
package GUIMain;
public class NumberSettingsFile
{
int maxSortBoxes = 50;
public int getMaxSortBoxes()
{
return maxSortBoxes;
}
}

org.openide.util.Lookup Cannot Find Any Classes Implementing

SQLUtils.java:
import org.openide.util.Lookup;
import java.util.ServiceLoader; // This doesn't work either
public class SQLUtils {
public static DBDriver getDriver(String prefix) {
for(DBDriver e : Lookup.getDefault().lookupAll(DBDriver.class)) {
System.out.println(e.getPrefix());
if(e.getPrefix().equalsIgnoreCase(prefix)) {
return e;
}
}
return null;
}
}
MySQLDriver.java:
public class MySQLDriver implements DBDriver {
#Override
public String getPrefix() {
return "mysql";
}
}
DBDriver.java:
import java.io.Serializable;
public interface DBDriver extends Serializable {
public String getPrefix();
}
Main.java:
public class Main {
public static void main(String[] args) {
DBDriver d = SQLUtils.getDriver("mysql");
}
}
This does nothing when running it, it cannot find any classes implementing.
What the program is trying to do is get the driver that is entered as a parameter for SQLUtils.getDriver(String prefix) (in Main.java).
For some reason I cannot get this to work.
I'm not familiar with OpenIDE Lookup mechanism, but I am familiar with the Java ServiceLoader mechanism.
You need to provide a file in the META-INF/services/ folder describing what classes implement specific interfaces. From the Java Docs describing the ServiceLoader class is this example:
If com.example.impl.StandardCodecs is an implementation of the
com.example.CodecSet service then its jar file also contains a file
named
META-INF/services/com.example.CodecSet
This file contains the single line:
com.example.impl.StandardCodecs # Standard codecs implementing com.example.CodecSet
What you are missing is a similar file that needs to be included on your classpath or within your JAR file.
You don't include you package names so I cannot provide a more direct example to help solve your problem.
I dropped the NetBeans API and switched to Reflections. I implemented Maven and ran it with IntelliJ. Works well for me.

Getting error in Eclipse: syntax error on token start identifier expected

I am getting a strange error while creating a simple thread program in JAVA using Eclipse. The code is:
package threadshow;
public class Thread_Show extends Thread{
public void run(){
System.out.println("Inside the thread");
}
}
class Thread_Definition{
Thread_Show ts=new Thread_Show();
ts.start(); //Getting the error here
}
I am getting error "syntax error on token start identifier expected" in the line ts.start();. Why am I getting this?
EDIT I have used the code from http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html#thread-subclass
Found a very bad mistake done my me. Forgot to add public static void main(String args[]) in the Thread_Definition class.
You can't start your method inside class. Create some method first.
Are you defining both of your classes in the same java file?. If so, you define both the classes in different java files naming Thread_show and Thread_definition. Then inside Thread_definition you can create an object of Thread_show and call its function.
ADD main method-public static void main(String[] args)
package threadshow;
public class Thread_Show extends Thread
{
public void run()
{
System.out.println("Inside the thread");
}
}
class Thread_Definition
{
public static void main(String[] args)
{
Thread_Show ts=new Thread_Show();
ts.start();
}
}

How to use methods from two classes in eachother in Java?

I've been looking around and I only found one answer which wasn't clear enough, to me at least.
I am building a very basic chat application with a GUI and I have separated the GUI from the connection stuff. Now I need to call one method from GUI in server class and vice versa. But I don't quite understand how to do it (even with "this"). Here's what a part of code looks like (this is a class named server_frame):
textField.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
srv.sendData(arg0.getActionCommand());
} catch (Exception e) {
e.printStackTrace();
}
textField.setText("");
}
}
);
This is a code from server_frame, srv is an object from the other class (server) which contains sendData method, and I probably didn't define it correctly so hopefully someone could make a definition of it.
On the other side class server from which object srv was made contains method using JTextArea displayArea from server_frame in this code:
private void displayMessage(final String message){
sf = new server_frame();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
sf.displayArea.append(message);
}
}
);
}
Yet again sf is an object made of server_frame and yet again probably missdefined :)
Hopefully that was clear enough, sadly I tried the searching but it just didn't give me the results I was looking for, if you need any more info I will gladly add it!
Thanks for reading,
Mr.P.
P.S. Please don't mind if I made terminology mishaps, I am still quite new to java and open to any corrections!
Some class must be building both of these objects--the GUI and the server--and it should make each aware of the other. For example, say the main class is ServerApplication. I'll use standard Java convention of starting class names with an uppercase letter for clarity.
class ServerApplication {
Server server;
ServerFrame gui;
public static void main(String []) {
server = new Server(...);
gui = new ServerFrame(server);
server.setGui(gui);
}
}
Each class should store the reference to the other as well.
class Server {
ServerFrame gui;
public void setGui(ServerFrame gui) {
this.gui = gui;
}
...
}
class ServerFrame extends JFrame {
Server server;
public ServerFrame(Server server) {
this.server = server;
}
...
}
I think you may be looking for the ClassName.this.methodName syntax. this in those actionlisteners refer to the anonymous class you created. If you used the above syntax you would be referencing the class that contains the anonymous class.
Or if you are looking for a private field in the class, you would do ClassName.this.privateField

Categories