Eclipse happily generates bytecode for Java classes that have compilation errors. The offending methods just throw exceptions when invoked at runtime.
Does anyone know how I can stop Eclipse from generating bytecode when compilation errors are present? I'd rather not have runnable code at all in the presence of errors.
As an example, consider the following code:
public class Err {
public static void main(String[] args) {
methodWithoutCompilationError();
// methodWithCompilationError();
}
private static void methodWithoutCompilationError() {
System.out.println("No error here, folks.");
}
private static void methodWithCompilationError() {
System.out.println("This method doesn't compile." + );
}
}
It runs fine, even with the compilation error. Only when I uncomment the second line of main do I see there was a problem compiling.
The reason Eclipse does this is that the compile errors might be resolved by editing another java source file. For example, if a reference to a method name in another class is misspelled, the fix could be that you correct the spelling of the reference or the fix could be that you change the name of the method. Eclipse doesn't know which you'll choose to do so it compiles the referencing class anyway in case you decide to change to other file. I'm not even sure it could otherwise know when to compile all of the classes again.
As a result, Eclipse will always compile the edited java source to the extent possible whenever you change the source file.
I'm guessing at some point you enabled the 'Continue launch if project contains errors' option. Try setting it to 'Prompt':
Try checking the "Treat above errors like fatal compile errors (make compiled code not executable)" checkbox.
Related
I figured the error was due to my Path or ClassPath being incorrect, but I used Java for months with no problem as I set the Paths when I downloaded Eclipse Neon. I also seen being in packages can cause it, so I created classes outside of packages and still got the error.
The weird thing is some classes I create run fine, but others won't, and they'll be the exact some code in the same package, but only have different names.
Here is the exact example, HelloWorld works, but parserpractice doesn't, and they are both in the exact same location:
package ParserPractice;
public class HelloWorld {
public HelloWorld() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
System.out.println("Hello");
}
}
package ParserPractice;
public class parserpractice {
public parserpractice() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
System.out.println("Hello");
}
}
My Path and ClassPath environment variables on Windows 7 are:
Path variable name: C:\Program Files\Java\jdk1.8.0_101\bin;C:\Program Files\Java\jre1.8.0_101\bin;
ClassPath variable name: C:\Program Files\Java\jdk1.8.0_101\bin
I changed those around constantly, but all my old code still runs, and some new classes still run. But for some reason some new classes will generate the error:
Error: Could not find or load main class.
How do I solve this error?
Can you change your path and classpath like this
Path variable name: %ProgramFiles%\Java\jdk1.8.0_101\bin;%ProgramFiles%\Java\jre1.8.0_101\bin;
ClassPath variable name: %ProgramFiles%\Java\jdk1.8.0_101\bin
I used eclipse on my laptop and it worked fine. I think the issue has something to do with how old my PC is. It's ancient. I'm building a new one in the next couple months.
So, I'm closing this question as I tried everything possible, and at this point I think it's some esoteric problem that isn't even worth trying to figure out. I might try and re-install eclipse, and see if that fixes it, but since I'm getting a new PC I might not even bother.
Thanks for your help.
You can check your path for referenced libraries.
Go to referenced libraries >Build path > configure build path and check whether all the paths are correct or not.
I was wondering if it's possible to include a jar in the classpath when compiling instead of executing. At the moment I am just checking to see if my PostgreSQL driver can be found
everything is in the same location for testing purposes so
program/
DriverCheck.java
DriverCheck.class
postgresql-longassname.jar
DriverCheck.java contains
public class DriverCheck {
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
System.out.println(Driver Found);
} catch(Exception log) {
System.out.println(Driver Not Found);
}
}
}
If I compile and execute this as normal
# javac DriverCheck.java
# java -cp ".:./postgresql-longassname.jar" DriverCheck
It works as I get the output
Driver Found
However if I try to compile and execute in this manner
# javac -cp ".:./postgresql-longassname.jar" DriverCheck.java
# java DriverCheck
It does not work as I get the output
Driver Not Found
Why is this and is there a way for me to use the second method for including jars?
Why is this and is there a way for me to use the second method for including jars?
It's because specifying the classpath for compilation just tells the compiler where to find types. The compiler doesn't copy those types into its output - so if you want the same resources available when you execute, you need to specify the classpath at execution time.
In this case the compiler doesn't need the extra jar file at all - nothing in your source code refers to it... but you do need it at execution time... which is why your original approach works and your second approach doesn't.
When I run the below code, I get the error that Could not find or load main class. I have removed the package and created it again. But the error is still exist. I did some methods to fix it such as right clicking on package name -> properties -> run option to change the main method but there is nothing. But if I create another package name and write this code in it, the program work.
package craps;
public class Craps {
public static void main(String[] args) {
int number = 10;
System.out.println(number);
}
}
Your code is not having any errors
I don't know what is happening in Netbeans .I have been using this for years and living with this kind of errors.
perhaps you get this when netbeans running out of memory and that particular moment you are editing this file.
My workaround for this kind of errors are
1.Do some dummy editing in that file like commenting some empty line // and save All and recompile it
2.Close and open this project (Sometimes work)
I have similar problem to the one described here:
Eclipse and Java - source not found
I also looked at the following question: Eclipse java debugging: source not found but I could not see how that it applied to my case..
I have just started using Eclipse and its debugger.
Here is how to reproduce the problem using Eclipse 3.7.2 on Ubuntu 12.04 with java and javac version 7.
Start Eclipse and select workspace, e.g., "Test" in home folder.
Open java perspective
Open new java project with project name "Test"
Add a new java class "Test"
I now have the following screenshot:
Add the following code to the source file Test.java
set a breakpoint at new Test2(1)
open debug perspective
start debugging:
choose Step Into (F5)
Now the error is reported:
Any help on this issue is appreciated..
The class Launcher$AppClassLoader belongs to the JRE and is about to load your class. It has nothing to do with the source code of your own classes. If you step further you will reach your own class Test2. If you go to the end of your debug button bar (four buttons right to the “step into” button), there’s a “Use step filters” button. Activate it to avoid unnecessary steps into the JRE classes.
I believe you have to create an instance of Test before you can access the nested class Test2 in Test. Eclipse should have thrown an error in yours saying something like "No instance of Test2 is accessible" or something like that. Change your code to look like this and see if it works.
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test mTest = new Test();
Test2 nTest = mTest.new Test2(1);
}
class Test2{
int i;
Test2(int i){
this.i = i;
}
}
}
I am trying to run this project called "hello user". I am new to Java, so wrote a simple program that takes your name, and displays "Hello ". while Running it, I get the following error:
run:
Error: Could not find or load main class hello.world.HelloWorld
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
But when I run file HelloWorld.java, it does it fine
I am doing this on Netbeans IDE 7.2
Rather than the coding error, it could be related to IDE. Since the "Run File" runs okay, but 'Run Project" does not, I believe you have something to set up in IDE itself. Right click the project, and select "Set is as Main", now run the project. I am just giving it a guess, may not help you. But it worth a shot.If it does not help, please paste your code too.
Your class needs a public static void main(String[] args) function. And moreover I suspect that the error could be in the package.
If you want your class in <main_package>.<sub_package>, The directory structure is
- main_package
- sub_package
-HelloWorld.java
And be sure to write your class like this.
package main_package.sub_package;
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello " + args[o]);
}
}
This is all due to the naming convention in Java
You need to run the .class file containing the public static void main(String[] args) method..
Here, your HelloWorld.java file might contain a class with main() method.. So, you can run it..
This is because, execution of any Java program starts with the invocation of main().. JVM needs an entry point to your code.. Which is main().. If it doesn't find one.. It will not run..
So, make sure, whatever class file you are running, it should have main() method..
UPDATE :- And for the starting point, may be you can skip using packages.. Just go with plain Java class without packages..
This message can also appear in Eclipse (Juno 4.2.2 in my case) and I have found two potential causes for it.
In my cases:
1. a DTD was in error. I deleted the file and that solved the issue*.
2. having cleaned the project, an external Jar that I had built externally had been deleted as could be seen from Properties -> Java Build Path -> Libraries.*
*Having solved either of the above issues, it was necessary to restart Eclipse
if you are using intellij idea then just rebuilding (clean and build) project might solve your problem . because intellij might be still trying to load the old classes which are not there or changed
Make sure you call looks like below:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello user");
}
}
To run a Java class in stand alone mode, public static void main(String[] args) is the entry method, which is must.