Can't load main class - java

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{}

Related

I messed up in Run/Debug Configuration and now unable to run a single line of code with IntelliJ

Here is the screen. Please tell me how to solve this.
And here it is saying main method is not found but I already written that in the class.
I tried to edit the configuration and messed it up. How do I fix this?
You have a class named String in your project. That class shadows the class java.lang.String.
That means that you must either write your main method as
public static void main(java.lang.String...args) {
}
or rename your class String to something else.

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.

Java Adding External Library failed when Self-defined Package Exists

Recently I have encountered some problem which seems a little strange to me.
In order to use some predefined class, I imported two .jar files say foo.jar and bar.jar(Both were written by others)
And my source code is like the following:
package jerry.deque
public class Deque {
.....
.....
Foo item = new Foo(); //Already defined in the foo.jar
.....
}
I added the external library exactly as what How to Import a Jar in Eclipse
did. But when I tried to use the class defined in foo.jar Eclipse shows me that "Foo can't be resolved to a type".
I spent a lot of time to fix this problem and finally succeeded after I removed
the clause: "package jerry.deque" at the beginning of my class file.
I think this is weird because just a few days ago when I was doing some Android development, I followed the same way to add a Twitter API library. And it works fine even when I declared "package jerry.search_twittes" at the beginning of my .java
file. I'm confused by this problem and couldn't figure out what's going wrong. Could someone help me to explain it in detail? Thanks very much.
Check that Foo is same package as Deque class. If they are not same
package, you need to import Foo class in Deque class.
For example,
package jerry.deque;
import packagename.foo; // packagename.foo
public class Deque {
.....
.....
Foo item = new Foo(); //Already defined in the foo.jar
.....
}
Added Explanation
I want you to check access modifier of Foo class carefully.
There are 2 access level for top level(class) access control . These
are public, or package-private (no explicit modifier).
Your Foo class is under default package(not specified package)and may be no explicit access modifier. Hope so! Then, all classes under default package can access to Foo class. That's why when you remove package jerry.deque clause, it works.
Similarly, I want you to check Android development java code in which it works fine even when you declared "package jerry.search_twittes". In that case, classes inside Twitter API library's access modifier is public.So you can access it from anywhere.
For more information you can read this.Is this information helpful???
Foo is in default package. Classes from default package cannot be imported directly.
So when you remove the package declaration in your code, you don't get the error.
You can look for reflection api or write a proxy in the default package for that class.

How to mention the class path

How can I mention the path of a class as in following code?
Class cl=Class.forName("SomeClass");
This works well if the "SomeClass" is located in the same directory of the calling class. But how to check for a class from a different directory, I saw the syntax for that is like xxxx.yyyy.class, but could not make out what those 'x's and'y's stand for. please help.
Thanks in advance.
Use the fully-qualified class name. For instance, to do this with the Java SE String class, which is in the java.lang package:
Class clazz = Class.forName("java.lang.String");
Those xxx and yyy stands for package names. Packages are normally represented by directories on disk with the same name as the package. When you create a class file you can specify which package the class goes by stating package xxx.yyy at the top of the file.
Referring to "SomeClass" without a package name will try to load a class named SomeClass in the default package.
Use Class.forName although make sure you deal with a possible ClassNotFoundException exception. This is a runtime exception so it does not mean you need to catch it. This is how you will know if you path to the class is correct. The problem is that if it cannot find the class funny things can happen with your code.
Class.forName(com.my.package.ClassName)

Classes in same package

I love the Intellij IDEA but i have been stacked on one little problem with Java imports.
So for example there is a package with name "example" and two different classes in it: A.java and B.java.
And i wanna get access to class "A" from class "B" without imports. Like this:
class A:
package example;
public class A{ ... some stuff here ...}
class B:
package example;
public class B{
public static void main(String[] args){
A myVar = new A();
}
}
This code may not work, but it's doesn't matter. Trouble just with IDE and with its mechanism of importing classes.
So, problem is that i can't see A class from B. Idea says 'Cant resolve symbol' but i actually know that class A exists in package. Next strange is that complier works fine and there are no exceptions. Just IDEA can't see the class in the same package.
Does anybody has any ideas?
If they are in the same package, you can access A in class B without import:
package example;
public class B{
public static void main(String[] args){
A myA = new A();
}
}
Maybe this will help you, or somebody else using IntelliJ that is getting a "Cannot resolve symbol" error but can still compile their code.
Lets say you have the two files that buymypies wrote up, the standard Java convention is that the two files would exist in an Example directory in your source code area, like /myprojectpath/src/Example. But it is technically not a requirement to reflect the package structure in the source directory structure, just a best practice sort of thing.
So, if you don't mimic the package structure, and the two files are just in /myprojectpath/src, IntelliJ will give you the "Cannot resolve symbol" error because it expects the source code structure to reflect the package structure, but it will compile okay.
I'm not sure if this is your problem, but I do use IntelliJ and have seen this, so it's something to look at.
I have the same problem as this: 2 classes in the same package, yet when one tries to call the other, Intellij underlines it in red and says Cannot resolve symbol 'Classname', e.g. Cannot resolve symbol 'LocalPreferencesStore'.
It then wants to add the fully qualified name in situ - so it clearly knows the path - so why can't it just access the class?
The module still compiles and runs, so it's just the IDE behaving oddly - and all that red is very distracting, since it isn't actually an error, it's just IDEA throwing a weird wobbly.
This is also recent. Two weeks ago I wasn't having this problem at all, and now suddenly it's started up. Of course, it could go away again on its own soon, but it's really annoying.
Same issue and I just fixed it.
I don't know your folder structure.
However, if your package example was added manually.That's the problem.
The package should be the same as your folder structure,which means if you want your class file to be stored in package example,you must store you java file in the src's subfolder named example.
You need to learn the basics about Java i think.
Here is a basic example of what i think you are trying:
package Example;
public class A
{
String myVar;
public String getMyVar()
{
return myVar;
}
public void setMyVar(String myVar)
{
this.myVar = myVar;
}
}
You need to create an instance of A.
package Example;
public class B
{
/**
* #param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
A myA = new A();
myA.setMyVar("Hello World!");
System.out.println(myA.getMyVar);
}
}
Look up java 'getters' and 'setters'.

Categories

Resources