Java program had compilation error yet ran successfully - java

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.

Related

Eclipse runs the program, same does not work when ran through command prompt

I was going through some basic java concepts. I was looking into methods and trying to verify this error
"MethodSignature.java:10: error: method m1(String) is already defined in class MethodSignature
public static void m1(String s)"
The above error appears when I run from command prompt. But when running through eclipse, although it shows error, program prints the desired strings. I do not get any error as stated above in case of command prompt.
Why there is a difference in execution in Eclipse and command prompt?
As in command prompt, I am not able to run the program itself because the error should stop me. I was expecting the same in Eclipse.
Here is my simple program.
public static void m1(String s)
{
System.out.println(s);
}
public static void m1(String s)
{
System.out.println(s);
}
public static void main(String[] args)
{
m1("call one");
m1("call two");
}
You must be running a previously compiled class, you cannot declare m1 twice. However, eclipse does have its' own compiler (ecj); and it is possible to run code ignoring errors (in which case it removes the invalid code for you).

IF statement in php Retains answer from previous run

I Have the following code.
$Case_Answer = 99.999;
$Compile = exec('javac Main.java');
if ((exec('java Main')) == $Case_Answer){
echo "Correct";}
else
echo "Incorrect";
My problem results when I run the script several times.
If I run this php script with all correct values and making sure that Main.java compiles. Everything works well.
When I run it a second time after modifying Main.java so it does not compile correctly. The if statement will execute whatever it did the previous run. I need it to refresh every time and it cause a problem.
Does anyone know how to resolve this?
All help would be appreciated.
If the first compile is successful and the second compile fails it still has the previous good compiled version to that runs yes? Then you may need to delete the working file before compiling again.
I'm not a Java guy so I will assume that the compiled Java file is called Main:
$Case_Answer = 99.999;
if(file_exists('Main')) { // delete it here
unlink('Main');
}
$Compile = exec('javac Main.java');
if ((exec('java Main')) == $Case_Answer){
echo "Correct";
} else {
echo "Incorrect";
}
Even if the second compile would otherwise be successful, won't it fail if the file exists from a previous compile?

Process.waitFor() not waiting

I am developing a Java application wherein I am compiling and runnimg C code. I am opening a new RedHat Linux terminal using the following code and running the a.out file.
public void run() throws Exception
{
String[] arg={"gnome-terminal","-e","./a.out"};
Process P=Runtime.getRuntime().exec(arg);
P.waitFor();
}
The problem I am facing is that P.waitFor(); does not work and the terminal shows the output and exits immediately, and I cannot see the output. If I put getchar(); at the end of C code only then I can see the complete output. I need to know a way through which I will not have to put getchar(); at the end and still see the output.

Stop Eclipse from generating bytecode for code that doesn't compile

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.

Different results when compiling with command prompt and BlueJ

I'm just starting Java ... again.
I just made a simple program
class first
{
public static void main()
{
System.out.println("Hello!");
}
}
This runs perfectly fine in BlueJ but it gives an error during run-time when running from command prompt.
This is the error
Exception in thread "main" java.lang.NoSuchMethodError: main
It's because I didn't give String args[] in the main parameter list
Till now, I used to give it subconsciously. I know that the string array contains all the parameter values when running but then why is it running in BlueJ?
(BlueJ is a student-friendly Java editor and compiler)
Your program is valid and will compile to the same thing whether you compile from BlueJ or from the command line.
However, blueJ will let you run any static method in a class (so you can test your functions) where as the command line java command will (only) look for a special main method to run. This main method tages a String array with all the command line parameters and your program should look like this even though you don't use these command line parameters:
class first
{
public static void main(String[] args)
{
System.out.println("Hello!");
}
}

Categories

Resources