Java - could not find or load main class error - java

I just added some code to a project and suddenly I get these errors. The class in question does have a main method and the other class ClientLauncher in the client package exists. I checked and the classes have the correct package listed in them. Any suggestions on what I should check? Thanks.
Error: Could not find or load main class client.ClientLauncher
selection does not contain a main type

I'm not sure if this is what was going on, because I had deleted the last project and started over. But I just got a similar error and thought I'd post this in case it helps someone. In the process of doing a similar project, I forgot to include "String args[]" in the main method definition and only had"
public static void main(). This gave a similar "does not have main method". The moment I added the above and had:
public static void main(Strings args[]);
then the error went away and the code ran correctly.

Related

What is the difference between java.lang.NoSuchMethodError main Exception in thread "main " and Error: Main method not found in class

I have this class
public class demo3 {
private static void sum()
{
}
}
when I tried to run this class, I expected the error to be java.lang.NoSuchMethodError main Exception in thread "main "
however, the output was a bit different and I got below message
Error: Main method not found in class demo3, please define the main method as:
public static void main(String[] args)
now this got my curiosity as to in which case I will get java.lang.NoSuchMethodError or in which case I will get the other error message.
You get the Main method not found message when public static void main(String[]) can't be found in the class that you've asked the JVM to start running. That is, the entry point of the overall program cannot be found.
You get the java.lang.NoSuchMethodError message if your (already running) code attempts to invoke a method on a class which was available at compile time, but not available in the version of the class you are using at runtime (for example, you compile against one version of the library, and then update the library jar without recompiling). This can occur at any point in the program.
There doesn't look to be anything in JLS which says that NoSuchMethodError can't be thrown, rather than the Main method not found; however, failing to write a main method (either entirely, or writing one with the wrong signature) is a far more common mistake than the "class changed after compilation" case, especially for beginners, for whom the NoSuchMethodError might be too cryptic. There is no harm in providing a more user-friendly message in this one case.

Java NoSuchMethodError when Method Exists

I am referencing PlayerUtil.getMovementSpeed(player); in my Speed class, and in my PlayerUtil class, I have the method defined as:
public static double getMovementSpeed(Player player) {
//my code here
}
But whenever the getMovementSpeed method is referenced in my other classes, it throws this error:
java.lang.NoSuchMethodError: net.Swedz.util.PlayerUtil.getMovementSpeed(Lorg/bukkit/entity/Player;)D
I thought it may be that Eclipse was exporting incorrectly, but I rebooted it and tried again with no avail.
EDIT: I did try decompiling the exported jar, and the public static double getMovementSpeed(Player player) method does exist in the exported jar.
EDIT: My friend is also having a similar issue, and is using IntelliJ, so Eclipse is not the issue.
EDIT: Class definition for PlayerUtil:
package net.Swedz.util;
public class PlayerUtil implements Listener {
//getMovementSpeed is defined in here
}
Class definition for Speed:
package net.Swedz.hack.detect.move;
public class Speed implements Hack, Listener {
//my detection methods and method containing PlayerUtil.getMovementSpeed(player);
}
SOLUTION: I found on my own that I had classes conflicting between two plugins on my server. I had one jar with net.Swedz.util.PlayerUtil and another with net.Swedz.util.PlayerUtil both with different contents. I added my project name in all lower case after the net.Swedz and it seems to have fixed it!
Thanks!
This is a very simple to troubleshoot.
you have used that method and you were able to compile that class which uses this method.
so that means at compile time it reefers the class PlayerUtil which has this method.
But runtime class loader has loaded the class PlayerUtil which doesn't contain this method.
now what you have to do is just find out where that class has been loaded from (at run time)
if you can recreate the problem while it is running using eclipse/IDEA follow these steps.
(if it runs in in application server or standalone application, then start the application server or application with debug enabled.and you can do remote debug from your IDE).
put a break-point where exception was thrown (where you call this method).
start to debug , it will hit the break-point.
then evaluate this expression PlayerUtil.class.getResource("PlayerUtil.class")
4.you can find the path where the class was loaded from.
now you have two options , decompile the class and check whether that method is these (same return type, same name , same args).
or in debug , you can evaluate PlayerUtil.class.getDeclaredMethods() to find out.
So you can solve the problem by rectifying the class path entries if it was loaded from a wrong place.

Receiving couldn't find class error even Setting Soot library classpath

I use soot for instrumenting a java program. I know for adding invocation to specific class in soot, we must set "Soot class-path" to the directory contains that class, .class file. So I do this in main method of main class. I bring the snippet of code bellow
public class Main {
public static void main(String[] args) {
Scene.v().setSootClassPath("/home/j/IdeaProjects/Test_1/classes:/home/j/IdeaProjects/Test_1/libs/rt.jar:home/j/IdeaProjects/Test_1/libs/jce.jar");
PackManager.v().getPack("jtp").add(new Transform("jtp.RetIns", new ExIns()));
....
But when I want to use "Insop" class which resides in classes folder, by following code in Exins method:
static SootClass Ins;
static
{
Ins= Scene.v().loadClassAndSupport("Insop");
}
I get the error
Caused by: java.lang.RuntimeException: couldn't find class: Insop (is your soot-class-path set properly?)
I should mention that I use ubuntu 14.4 32 bit and I run the code on intellij.
I can not find what is my mistake. could you please help me.
I finally find the problem. I don't know why, but I should set "soot class-path" with relative path. For my project for example it should set as follow:
Scene.v().setSootClassPath("classes:libs/rt.jar:libs/jce.jar");

NoClassDefFoundError exception in my thread

I get this error:
Exception in thread "http-server" java.lang.NoClassDefFoundError: org/w3c/jigmonitor/ReplyServer
but I don't undestand why. Could someone explain why does this happen?
This is the class that causes the problem:
public class ReplyServer implements Serializable
{
public ReplyServer()
{
super();
}
}
It looks like the class you're defining isn't being found by something that's trying to load it. In my experience this is caused by one of three problems:
Your package declaration for the class is not correct. In this case something on the http-sever thread is expecting your class to be declared in the package org.w3c.jigmonitor.
Your source file is not located in the correct directory. In this case, your source file should be located in a directory structure like "org/w3c/jigmonitor/", providing that's the package you actually want.
The path of the compiled class for ReplyServer is not in the classpath of your JVM. You can check this by looking at the classpath used to start your JVM and seeing if the class is actually there or not. In most generic setups servlet setups there will be a "WEB-INF/classes" folder for you to go poke around in.
Good luck!
(The link David posted gives a ton of information on this type of issue and the possible causes. I would recommend tucking that away for later)

Xcode cannot find a java class which I'm trying to extend

I'm working on a Java project which was written by someone else. This person made a hierarchy of folders inside the 'src' folder. I've added a new java class into one of those folders and defined it as 'XmlFile.java'.
Then, I'm trying to have it extend a previously written class 'GenericFile.java' by writing
package //Same package GenericFile is in
public class XmlFile extends GenericFile
{
...
}
When I try to compile the project it gives me the error
Cannot find symbol
and refers me to the line
public class XmlFile extends GenericFile
if I take out
extends GenericFile
everything compiles great.
I also notice after adding the new file (XmlFile.java) I cannot delete it (the option in Edit->Delete is not selectable for that file, or for any files/folders created by the person from whom I got the project).
Is there some sort of permission issue here or some hidden scope issue caused by the permissions being strange or what?
Please help me
Cheers,
WhiteTiger
I admit I am not an expert enough to figure out "Cannot find symbol" from a Java compiler, but since there is no other answer, here is a sneaky idea -
If GenericFile is a working and useful abstract class, there has to be at least one other class (hopefully working) that extends it. Since you have the source code, find out one such file, copy it to XmlFile.java, edit it to change the constructor name to XmlFile and try to compile it. If it compiles, start from there. If not, you will know where the problem lies.
Just trying to help! Good luck, - M.S.

Categories