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.
Related
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.
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 :)
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.
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.
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'.