Sub: error messages are displaying different in both eclipse and command prompt
//DataHidingDemo program
class Bank {
private static double balance = 1000;// Data Hiding
}
public class DataHidingDemo extends Bank {
public static void main(String[] args) {
System.out.println("Balance:" + balance);
}}
Case 1:
run the program from eclipse and observe below error message is displaying
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field Bank.balance is not visible
at oops.DataHidingDemo.main(DataHidingDemo.java:10)
Case 2:
run the same program from command prompt and observe below error message is displaying
D:\Java Programs_CMD>javac DataHidingDemo.java
DataHidingDemo.java:10: error: balance has private access in Bank
System.out.println("Balance:"+balance);
^
1 error
Observe both case 1 & 2 error messages; case 2 error message has meaningful.
Q).Do we have the way to display the same error message in eclipse too? (for this do we need to change any setting in eclipse) please help on this.
Eclipse has its own Java compiler, which is thus different from javac, and thus generates different error messages. AFAIK, no, it's not possible to make Eclipse use the javac compiler. Both NetBeans and IntelliJ IDEA use javac, though, so you might want to try thise IDEs instead.
Note that the error you got from Eclipse is a message generated when you tried running code that did not compile. Don't do that. If there are compilation errors listed, then fix them all before running. The compilation errors are listed in the "Problems" view and in the "Markers" view of Eclipse.
Related
class Return
{
public static void main(String args[])
{
boolean t=true;
System.out.println("Before the return");
if(t)
return;
System.out.println("This wont execute");
}
}
This program is from Herbert Schidt. I tried to run this program using command prompt without if(t)
to see the compilation error
error: unreachable statement
System.out.println("Wont Execute");
I understood this error but program is running fine when i execute the command java Return.
It shows the output
Executes
So I wanted to know how this program is running even with compilation error?
You were almost certainly running the last successfully compiled version of this class. When you execute the java compiler, it doesn't erase its old output, it overwrites it. So if you compiled Return.java once successfully, you'll have a Return.class on your disk. If you alter Return.java to be uncompilable and try to compile it, the Return.class from the previous successful compile will still be there.
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)
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.
I'm using Eclipse for Java on my Macbook air, OS 10.9. I keep getting the error Obsolete Methods on the Stack, with a warning about how it may cause problems with the virtual machine when I run very basic programs that have no errors. I ran a program that just had the class and the main method and I got this error. After the error box, the bug referred to the main method, but I know the syntax is correct because I used Eclipse's main method.
import java.util.Scanner;
public class Dowhile {
public static void main(String[] args) {
/*Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int value = scanner.nextInt();
System.out.println(value);*/
/*do{
System.out.println("Enter a number ");
int value = scanner.nextInt();
}
while(value != 5);
System.out.println("Got 5");*/
}
}
Update:
I'm not getting the obsolete methods error now, just Exception in thread "main"... at line 5.
This error message indicates that you are doing hot code replace, and that
the frames on the stack no longer match the class files in the running VM.
Restarting your debug session/target VM should suffice.
Hot code replace (HCR) meaning:
It is a debugging technique whereby the Eclipse Java debugger transmits new class files over the debugging channel to another JVM.
read more.
Bugzilla
Dreamincode
I clicked on Terminate button and it no longer showed that warning message again.
Google analytics posted on their blog about an example Java program to extract data and output to a CSV using the analytics api, im trying to compile this program to run it in eclipse but running into problems
http://code.google.com/p/ga-api-java-samples/source/browse/trunk/src/v2/DataOverTime/src/example/MainDemo.java
i have changed out the variables in here to my own:
// User configurations.
private static final String USERNAME = "INSERT_YOUR_GOOGLE_ACCOUNT_LOGIN";
private static final String PASSWORD = "INSERT_YOUR_GOOGLE_ACCOUNT_PASSWORD";
private static final String TABLE_ID = "INSERT_YOUR_TABLE_ID";
i'm unsure on the next step, when i try to run the program i get this error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at MainDemo.main(main.java:51)
update:
ok, its says when using the google analytics API i need to reference some .jar files:
gdata-core-1.0.jar
gdata-client-meta-1.0.jar
gdata-client-1.0.jar
gdata-analytics-meta-2.0.jar
gdata-analytics-2.0.jar
google-collect-1.0-rc1.jar
jsr305.jar
i put these in but its still not working i get the following error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Results cannot be resolved to a type
The method getData(DataQuery) from the type DataOverTime refers to the missing type Results
Is your directory structure correct? It should look somewhat like the url of the example.
The file has to be './example/MainDemo.java' and compile it from '.'
I got the answer in this forum: http://code.google.com/apis/analytics/groups/exportApiGroup.html?place=topic%2Fgoogle-analytics-data-export-api%2FwpDmftjYFio%2Fdiscussion