Every java program I try to start shows error - java

SOLVED, Program was in location with national symbol in it's path.
I just started studying java, but every program i try to start (even example ones from my course) shows an error.
Error: Could not find or load main class "Any class name of program I try start"
C:\Users\Mine\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
edit:
example of code, but happens to any code.
public class Hello {
static void hello(){
System.out.println("Hello, World!");
}
public static void main(String[] args) {
hello();
}
}

This error means that when Netbeans is invoking the JVM, the JVM cannot find the class file for the class Netbeans is telling it to run. When you create a project in Netbeans, the classpath will be configured for you by the IDE, so you shouldn't normally see this error unless you have deleted the auto-generated main class and made a new one from scratch in the wrong place.
So the first thing to do is check what class Netbeans is using as the main class:
Right-click on the project name in the Projects tab and click on "Properties"
Then click on "Run" and check the name of the class in "Main Class":
Note in my example the class is called "tests.Test". This means the class Test in the package "tests". In your question, your class "Hello" doesn't have a package declaration at the top (although you may have chosen not to copy this). If you have no package (and you really should be using packages, even for trivial programs like "Hello, World!", just to get used to doing so, if nothing else), the "Main Class" entry should just be the class name.
So you need to either move your class into the package specified in this parameter, or change this parameter to match the fully qualified name of your main class

Error: Could not find or load main class "Any class name of program I try start"
C:\Users\Mine\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
You are attempting to run a class called Any class name of program I try start, however the name of your class is Hello.
I don't know how Netbeans does things, but I would first try compiling and running the program without netbeans.
javac Hello.java
java Hello
If that works then open up the run settings in netbeans and make sure that it is doing the same thing.

Just make a new main class or just re-type public static void main(String[] args) { } and that's it.

Related

How to run a java .class file that extends a Class which is in another directory?

I first met this problem in my Ideas, I wrote a Class that extends javax.servlet.http.HttpServlet, My Ideas throws an error message reads Error: Could not find or load main class com.bjpowernode.OneServlet, here is the image:
enter image description here
It can be seen that Idea didn't show the red wavy line, that shows my codes are fine. I found a solution to this problem from enter link description here, I changed the scope from provided to compile:
enter image description here
But I actually want to know why and how it works? I compared the difference of Idea compilation instructions under different scope setting,I found that when Idea uses java command to run the .Class, the parameter -classpath of the command of the compile scope has two more paths:
D:\apache-tomcat-8.0.50\lib\jsp-api.jar;D:\apache-tomcat-8.0.50\lib\servlet-api.jar
That's to say, Idea didn't consider external library paths when run .class under the provided scope, and the super Class HttpServlet is from servlet-api.jar package. Why?
To simplify the problem, I created two different classes under two different paths and packages: Class Base and Class Sub, and Sub extends Base.
The codes of the Base is here:
package base;
public class Base{
public static void main(String[] args) {
}
}
The codes of the Sub is here:
package sub;
import base.Base;
public class Sub extends Base{
public static void main(String[] args) {
System.out.println("In Sub");
}
}
The path of Base is ./path1/base/Base.java and the path of Sub is ./path2/sub/Sub.java.
I compiled them using these two commands:
javac ./path1/base/Base.java -d ./path1
javac ./path2/sub/Sub.java -d ./path2 -cp "./path1;./path2"
And compiled successfully. But when I run sub.Sub using the command below:
java sub.Sub -cp "./path1;./path2"
And I got the same error:
Error: Could not find or load main class sub.Sub
Ive tried multiple variations of this, but none of them seem to work. Any ideas? Although I solved the problem of idea reporting errors, I still could not understand the principle behind? I hope this question can help me to figure it out. My jdk version is 1.8. Thanks in advance.

Why doesn't obj.start() run when I run my code?

I'm learning java and practicing on sololearn.com and I copied one of the examples to practice typing code. However the code here -
//Create myClass
class Loader extends Thread {
public static void main(String[] args) {
}
public void run(){
System.out.println("Hello Young World");
}
}
public class MyClass {
public static void main(String[] args) {
Loader obj = new Loader();
obj.start();
}
}
isn't printing "Hello Young World" to my console. In fact I had to add a 'main()' method to the Loader class just to run MyClass.java. However in the example their code ran without having to include a main method in Loader. Maybe they have customized their environment to allow for this type coding and IntelliJ just has different rules. Please could someone copy the code on their machines and run it with IntelliJ to see if they run into the same problem?
I've troubleshooted, but the code seems to be solid.
I guess you start the wrong main-method (the main from the Loader class which is empty). As you noted, you don´t have to add a main-method in your Loader class. Please remove the method and start the main-method from MyClass.
If you have several main-methods, you can choose which one should be executed in the run configurations. In your example, it should look like this:
In this case, make sure you select the one you like to execute.
Another way to execute the right main-method, is to select the class which contains the main-method and hit the play button on the left side:
To run the program in my console I had to 'java MyClass' rather than running 'java MyClass.java' which executed the whole file rather than just the class whose main method I wanted to call!
To run the program in IntelliJ I had to make sure that the right Class was being called as pointed out above.

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.

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

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