why cant find or load main class when extends TestCase class - java

I encounter a interesting error by accident and I need you help me figure it out.
as for the below code every thing is ok:
public class HelloWorld{
public static void main(String[] args){
System.out.println("hello world");
}
}
then as for the follow code, something interesting come.
import junit.framework.TestCase;
public class HelloWorld extends TestCase{
public static void main(String[] args){
System.out.println("hello world");
}
}
when I run it, it says
cant find or load main class.
I know when jvm load HelloWorld class it will load TestCase class first. So I make the below code segment.
public class HelloWorld {
public static void main(String[] args) throws ClassNotFoundException{
Class.forName("junit.framework.TestCase");
}
}
Exception in thread "main" java.lang.ClassNotFoundException: junit.framework.TestCase
I dont know why? Maybe the TestCase class is not on my classpath ? But as for the second code segment I ensure TestCase is on my classpath.
So my question is:
Why can't I load TestCase class?
Why can't it find or load main class when extend TestCase class?
Note: I know the print helloworld function has nothing with TestCase class which used to junit test but I want to figure out the reason that bring this suprise.

I dont know why? Maybe the TestCase class is not on my classpath ? But
as for the second code segment I ensure TestCase is on my classpath.
Class.forName("junit.framework.TestCase");
returns the Class object associated with TestCase class. It doesn t set the classpath.
That's why you rise the exception :
Exception in thread "main" java.lang.ClassNotFoundException:
junit.framework.TestCase
You try to load a class which is not in the classpath.
To solve your problem with Eclipse, go in the properties windows of your project, then at the left, you have the Java Build Path option, go inside and look at the libraries tab. Here, you should add the JUnit library if you want JUnit to be in the classpath.

Related

java filename reference compile error

package concurrencyTest;
public class concurrencyTest implements Runnable
{
#Override
public void run()
{ System.out.println("Hello from a thread!"); }
public static void main(String[] args)
{
concurrencyTest c = new concurrencyTest();
Thread t = new Thread(c);
t.start();
}
}
Hi, I'm just trying to get my java concurrency test to run. But I'm getting this error :
Error: Main method not found in class concurrencytest.ConcurrencyTest, please
define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
I'm guessing that somewhere, in the myriad of project directories and subfiles that a java program needs to run, the name of the project or class has been wrongly referenced in lowercase letters. I've manually been thru all the files I can find that span from the root directory and renamed any instance of lowercase 'concurrencytest'. But still it seems the compiler finds a reference to lowercase concurrencytest and so refuses to compile. Any idea where this reference may be?
My root directory, source directory, and java code file are all called 'concurrencyTest'
edit
amended the original code for this question to include 'static' in the main method definition. Doing this was necessary but has not fixed the problem.
You wrote the main definition wrong. You should change it as below:
public static void main(String[] args) {
// ...
}
I found a reference to 'concurrencytest' in the meta file ...\concurrencyTest\nbproject\project.properties
So I changed it to 'concurrencyTest'
Also when NetBeans can't find the expected main method in the class it expects, it opens a 'RunProject' window where it lists main methods from classes it has found. For me it found a main in 'concurrencyTest' so I selected it.
One of these actions solved the problem, but not sure which one.

Why does the compiler say "main class could not be found or loaded" when main(String[] args) is a method?

Take this code for instance
public class Hello
{
static void main(String[] args)
{
System.out.println("Hello World");
}
}
Here, I didn't write public for the main method and compiled the class. When I run the program why does the error read as "Could not find or load main class Hello.java".
My question is, if main(String[] args) is a 'method' then why say 'main class'?The point is not that public is there or not. The point is that I changed access modifier which caused main(string[] args) to be invisible to JVM. So why does JVM say main class and not main() method?
P.S. If this a stupid question then I really regret asking it.
Edit:-
Here is the error message
Error: Could not find or load main class Hello.java
There are two problems here.
main() must be declared as public static void.
However the real problem was your command line. Clearly it was
java Hello.java
It should have been
java Hello
There is no such class here as Hello.java. The name of the class is Hello.

Change the system class loader at runtime

Main class
public class Main {
public static void main(String[] args) {
// Class load
A a = new A();
a.msg();
}
}
A class
public class A {
public void msg() {
System.out.println("msg");
}
}
I have written code in the main class that calls a msg() method of class A
After I created the jar file, I pull out A.class.
Then the path will have a jar file with missing A.class, and A.class.
A a = new A();
a.msg();
How do I dynamically load and run A.class without making any changes to the above code?
Please help me..
Looks like you want to load a class dynamically. I would recommend you to create a jar and load it using URIClassLoader. There is a really good answer here:- How should I load Jars dynamically at runtime?
A.class has to be in the classpath somewhere for the code to run. you can put it in a jar of its own, and add the second jar to the classpath. Then the class loader can find it.

Error - Main Class not found

Can't seem to figure out why this simple program has stopped working on my computer. My programs that I compiled up until a Windows update are fine but when I try to compile a new program I get the following. I've attached a picture of the program and the command prompt error message. I've checked the CLASSPATH and it looks fine.
Program - HelloWorldApp.java
class HelloWorldApp{
public static void main(String[] args){
System.out.println("Hello World!"); //Display the string
}
}
execute javac HelloWorldApp.java - all is fine
execute java HelloWorldApp -
Error: Main method not found in class HelloWorldApp, please define
the main method as: public static void main(String[] args) or a JavaFX
application class must extend javafx.application.Application
Please excuse me if this has been answered. I searched but couldn't find this problem
Have you declared a String class among your list of classes? If so, try using java.lang.String[] args as argument to your main method.

Main method not found even if I've declared it

I want to create a simple java class, with a main method, but when I compile my code, I get this error message :
Error: Main method not found in class errors.TestErrors, please define
the main method as: public static void main(String[] args)
This is the source code :
package errors;
public class TestErrors {
public static void main(String[] args){
System.out.println("hello");
}
}
Why I'm seeing this error, as you can notice I've alreader declared the main method !
As said in my comments, looks like you've declared a String class among your own classes. To prove this, I've created a basic example:
class String {
}
public class CarelessMain {
public static void main(String[] args) {
System.out.println("won't get printed");
}
public static void main(java.lang.String[] args) {
System.out.println("worked");
}
}
If you execute this code, it will print "worked" in the console. If you comment the second main method, the application will throw an error with this message (similar for your environment):
Error: Main method not found in class edu.home.poc.component.CarelessMain, please define the main method as:
public static void main(String[] args)
This usually happens if ur complete project isnotconfigured correctly
or one of your class in project has still some errors
in such cases IDE will prompt stating the same that project contains some error
and you still proceed (ie run your class)
as project has some bugs new classes will not be created and IDE will run
the class which was available previously
to make sure this is ur case u can add new class in your project and try to run it and if ur getting no such class exist then there its is a perfect evidence
Just check your java file, it has not been saved. Please save all java files before compiling.
If the answers above are not working for you: make sure "main" is not capitalized in your method definition.
OK:
public static void main(String[] args)
ERROR:
public static void Main(String[] args)
Though the error message makes the required syntax for the main method clear, incorrect caps can be hard to spot. It took me ~30 minutes of troubleshooting to find this typo today. This was clearly not an issue for the OP, but is another easy/likely way to produce the same error message, and this answer may help other users.
I had this issue just now. This error came up because the JRE file that i switched out didn't had the full library. After I corrected/added the right JRE system library the problem went away.
Right click on the class name (which you are trying to run)->Run As->Run Configurations->Under Main Class Tab
Write your main class name and hit on Run.
Try commenting the first line.
//package errors;

Categories