java.lang.ClassNotFoundException after cloning from github eclipse - java

Yesterday I uploaded some project to my github It compilled fine, Today I'm trying to clone it from other computer, I followoed this guide, but when I'm trying to run the code I'm getting:
Error: Could not find or load main class gui.MainScreen
Caused by: java.lang.ClassNotFoundException: gui.MainScreen
I tried to do like this post, but I didn't understand his answer, can someone please post clearer answer? Screenshots will help a lot.
Code roughly as follows:
package gui;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainScreen extends JFrame
{
public MainScreen() throws IOException
{
....
this.pack();
this.setVisible(true);
}
}
public static void main(String[] args)
{
//avoid blocking the main thread
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
try {
new MainScreen();
} catch (IOException e) {
e.printStackTrace();
};
}
});
}
}

You need to have the main class in a separate file. From your screenshot, it looks like you have the main class within the MainScreen class. You can name the file that has the main class anything you want (don't name it main though, just to avoid confusion).
If your main class is indeed in a separate file, set the main class after cloning your project in eclipse. Check this link How to set the main class in Eclipse. I am not sure that information is present in the github project that you cloned. Go to Run Configurations and setup the fully qualified main class name and as the link says, you can also search the whole project, which will give you the main class. Set that and then run the project. Hopefully, that will work.

I do not know if this answer will be beneficial to you or not but the same issue happened with me. To fix this what I did was just deleted the project from workspace and again imported it back from the git folder and everything started working fine.

My suggestion for those who encounter this problem, is to create a new draft project in Eclipse, see that it's running well, and then compare it to the
project > preference > java build path
that there is there (by edit you can see the settings of the working project vs the not working).
You can use screenshots to be sure.
When we are cloning a project it makes sense that some setting of the project are not fit to our local Eclipse setting, so we need to handle that.

Related

Error class not Found fixed but I don't understand why

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.

Why do I need package line in my class file while coding java in Eclipse?

So my first time using Eclipse doing an elementary program. I noticed that in Eclipse, you cannot compile a single class file. Rather you need to create a project on top of that. So I did create a project and created a class under the project. I noticed the code
package PackageName;
at the top of the class file. And if I delete the file and run the file, it gives me errors. May anyone answer me why is this happening? Thanks.
My code:
public class CSYes {
public static void main(String[] args)
{
System.out.println("Computer Science, Yes!!!!");
System.out.println("=========================");
}
}
Error Message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at proj1.CSYes.main(CSYes.java:3)
However, If I have
package proj1;
public class CSYes {
public static void main(String[] args)
{
System.out.println("Computer Science, Yes!!!!");
System.out.println("=========================");
}
}
It works perfectly fine.
The Eclipse IDE encourages you to use packages. In general, it's a good idea. I'd encourage you to use packages, too.
It is NOT, however, a requirement. It sounds like you inadverantly created a "proj1" package when you created the project and/or .java class. Whoops!
To fix the problem, simply a) delete the package reference in your .java source, then b) move the .java file OUT of "/src/proj1" and put in directly under "/src" (the "default package").
... OR, EASIER ...
Delete the entire source (both CSYes.java and proj1)
File > New > Java Class > Name= CSYes; leave package "blank" (i.e. "default package")
Copy/paste your code back into CSYes.
Voila! Done :)

Eclipse gives error for 'undefined' Java function it can easily find

I've been having trouble with Eclipse. I'm working on a java project that's configured with git. After submitting my code through git, Eclipse suddenly generates all these errors, saying many functions are undefined. However all the functions are still there, Eclipse will even take me to them when Ctrl+click the function.
For instance say I have this class:
public class myClass {
public myClass () {}
public void myFunction () {
//do some stuff
}
}
And now I have another class that uses it:
public class secondClass {
public void callFunction () {
myClass a = new myClass();
a.myFunction();
}
}
The myFunction call in secondClass causes an 'undefined' error in Eclipse. I've tried refreshing the project but it doesn't make a difference. The only way I've found to get Eclipse to behave correctly is to comment out the function it can't find, uncomment it, and then save the file. Is there a better way to do this? Or prevent Eclipse from having this problem?
Sounds like something is going wrong with your eclipse.
When this happens I typically do the following. (Continue to the next bullet if previous did not help):
refresh the project. First try F5. If it does not help do it using right click on project and choosing the appropriate option in context menu. It is strange, but sometimes F5 does not work, but menu does.
rebuild the project.
Try to close project and open it again
try to delete project (without removing content) through Eclipse.
try to create project again and copy *.java files there.
try to create new workspace.
I hope that #1 (or probably #2) will help.
Check if you have properly imported myClass in secondClass.
Secondly, your class naming is wrong. Class names generally starts with a capital letter.
From your 'solution' it seems to me the imports have gone missing. Commenting and then uncommenting the method and saving the file caused eclipse to fix your imports.
You have to take a look at the way you commit/push your code through git (and who did the last change), there is probably something wrong in the workflow that causes them to commit a file with missing import statements.

How to run Java .class file from another .class file? (java newb)

I've been running different individual Java .java files in the Netbeans IDE by right-clicking the .java files themselves in the Project Explorer of Netbeans (the portion normally at the upper left part of Netbeans).
However, i've been googling on how to make a class file run another class file using code, but to no avail.
I have a project named "loadanotherfile" with 2 files, namely: Loadanotherfile.java and otherfile.java
I'm trying to make Loadanotherfile.java run otherfile.java, but I'm not exactly sure how. I read about Classloaders and URLClassloaders however these methods don't seem suitable for my purpose of running another .java file.
Below is the code of the 2 files i mentioned.
Loadanotherfile.java
package loadanotherfile;
public class Loadanotherfile {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
// TODO code application logic here
}
}
otherfile.java
package loadanotherfile;
public class otherfile {
public static void main(String args[])
{
System.out.println("This is the other file.");
}
}
I have a feeling that the task has something to do with using the "import" syntax (namely something like import loadanotherfile.* but even if my guess is correct, I'm still not sure on how to make my Loadanotherfile.java run otherfile.java using code.
How can I load otherfile.java using Loadanothefile.java?
Cheers
In Loadanotherfile.java
otherfile.main(args);
Compile the two together, and then from Loadanotherfile,
otherfile.main(args);
will do the trick. You don't need to import since you're in the same package. Note the linked tutorial.
I would investigate (however) class instantiation, and creating an instance of a new class to invoke upon. Invoking static methods from static methods isn't very OO.
Try This:
className.main(Args){
}
This works! ive tested it myself.
Check the public void main line. If there IOException and not there then insert
in Loadanotherfile.java
use this
otherfile.main(args);{
}

Netbeans debugger unable debug any project with a particular class name

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.

Categories

Resources