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

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.

Related

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 :)

Can't load main class

It's a simple class and I am a beginner with Java.
I don't know why this code is not running and why it gives an error :
Could not find or load main class
class tuto{
public static void main(String[] args){
System.out.println("Hello World");
}
}
There are a couple things which jump out at me when I look at your question.
The first thing is that you have unresolved compiler errors. If you see that red 'x' on the Problems tab, you should fix all the errors there before trying to run anything.
The second thing is that your class name doesn't match the file name in which it is defined. For public classes the name of the class and the name of the file must match, and while your class isn't public, this is a widely followed Java convention and you will confuse people if you don't follow it.
As to your actual question, my best guess is that you have placed your class into a package and not declared it as such in your source code. If you go look at the Problems tab, it will tell you what is wrong and (often) how to fix it.
I can approximate your error message if I do the following:
In this case, I have an error over in the Problems tab complaining about the declared package.
Check to see if you have something similar:
If you do, you can right-click the error message and select "Quick Fix", and eclipse will pop up a dialog offering to add the package declaration for you:
In your code there is a compile error, that is because Syteme change it to System
Syteme.out.println("Hello World");
should be
System.out.println("Hello World");
P.S
And in Java when you have a public class in a file, then file name must be that class name. It is a must. Otherwise you will get an error.
If you have this class in a package then you must specify the package declaration first
e.g
package abc;
System.out.println not Syteme.out.println.
In Java (as somebody has already pointed) the name of the file should be of the same name of the main class within the same file.
Moreover, you should also declare an array using this syntax array_type [] array_id and not array_type array_id [].
There might be a couple of problems:
If the class is in a package, make sure you specify it. eg: package com.pak;
The class with main method always needs to be public. public class apples{}

Type A is already defined error

I tried to search for the solution, but what I found I don't know how to apply in this situation. Please help me correct my code.
package Exercise;
public class Ex11_11 {
public static void main(String[] args) {
A a = new A(3);
}
}
class A extends B { // type A is already defined, A has a red underline
public A (int t) {
System.out.println("A's constructor is invoked");
}
}
class B { // type B is already defined, B has a red underline
public B () {
System.out.println("B's constructor is invoked");
}
}
Eclipse sometimes gets confused. If you choose Clean from the Project menu, it might fix these errors.
Well, the first thing to check is obviously whether or not you have another class called A in your file or in the same package.
I had the same problem. My computer was restarted remotely by I.T, and Eclipse did not shut down gracefully. I noticed there was an extra java file in my project that I didn't add. Deleted it, and now the error is gone.
In Project-> Clean, select "Clean projects selected below", select my project(s) and check "Start a build immediately" with "Build only selected projects".
Then problem will resolve.
Check if all your class files are saved. I've had this problem a few times: i define a class in a class file then move it in it's own one. Java gets confised, because it reads from the old version of the original file. Once you save it with the missing class definition in it and define the class in the new file all should be ok.
In your project you might have test directory with the same package structure and the same class name (for example copied without changing class name to *Test).
If none of the above solutions worked for you then it possible that Build Path is messed up. When you add a src to the build path make sure the src is not in the exclusion list. Place *(wild card) in the inclusion list and nothing in the exclusion list.
Make sure
Project | Build Automatically
is checked.
The main reason for this is that somewhere in same package you have already defined a class with name A.
which causes type A is already defined error.
check if there is any subclass or inner class named A
Have you added another project to the build path?
I had the same issue on my development environment (Eclipse).
My Maven application consumed other applications. On Eclipse, those projects were just added to the build path. One of them had the same package structure and class filename. Eclipse ends up thinking both the files which are physically different are in the same directory since the package structure and filename are same.
For example, let's say there are two files as below:
/Users/uname/home/proj1/com/app/proj/main/java/util/file1.java
and
/Users/uname/home/proj2/com/app/proj/main/java/util/file1.java
and lets say both have the package name
com.app.define.proj.util
If you add one of the project to the other, Eclipse would consider both the files to be in the same location.
I resolved by creating a JAR file out of the consumed application, adding it to the build path and removing the Eclipse project from build path.
rename the class name A to Aa or something and try to run.

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