Skipping Java compilation errors [duplicate] - java

This question already has answers here:
Ignoring Compilation errors - Java
(7 answers)
Closed 9 years ago.
Is it possible to Skip Java compilation errors?
So the program may continue even if it encountered errors.
So it kinda does the max effort to continue working (until everything explodes)

No. If you get compilation errors, it means your program is not valid Java. What would it even mean for it to continue? It's not running yet.

If you have compilation errors, the program cannot be compiled to runtime code. That means practically that the program cannot be run.
So, the answer is NO, it cannot.
There is another type of errors - runtime errors. Those happens as a result of bad logic in the code (i.e. trying to modify a null object). Compiler cannot discover those as those are very subtle, depending-on-data (and most of the time depending on user input) errors. You can skip over those with the usage of a try-catch block. If you want to read more, please refer to this article:
http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html

Yes, you can do that using the eclipse compiler. It will replace the method which contains the error with a method that will directly throw an Exception when it is invoked.
This will allow you to run the rest of the program. I would not recommend it though, as you'll get an inconsistent program. So use at your own risk.
Still it might come in handy, for example if you run unittests. This allows you to run tests, even if parts of your program are definitely not working.

No its just not possible. Compilation of java code gives you the byte code which is then run on JVM. If the compilation will fail then Java compiler will not be able to generate valid byte code. Thats the reason that there is no point in generating the incorrect byte code which can't be run by JVM as such.

if we break that thing then sooner or later no one will be develop the code. :)

Well ... kind of.
Some IDEs allow you to run code that has compilation errors in it. For example, Eclipse.
When you then try to execute (say) a method with a compilation error it, you get an exception whose message says something to the effect that there is a compilation error in this code. In fact, the Eclipse compiler has compiled the method to a stub method that will just throw an exception.
Note that this behaviour is NOT endorsed by the JLS. And personally, I think it is a bad idea to use this IDE feature.
IMO, a better approach is to comment out the parts of the code that are giving the compilation errors ... or just fix them.

Your expecting to avoid compilation errors, what do you thing ?if you avoid compilation errors will you get .class file? without .class file you cant imagine to run you code.
compilation errors arise when your code contain syntactical eroor. without proper syntax you cant do anything.
For more about compilation error see THIS

Related

jshell finishes silently if the user tries to source a script with some error

Follow these steps to reproduce:
Create a '.jsh' script with some kind of error(missing import, syntax error etc)
open a jshell
/open the erroneous script
The /open command will finish silently.
The elements declared in that script won't be in the current namespace.
Is there a way to force jshell to spit out the error and it's location in the script if an erroneous script is loaded? Setting the feedback level to verbose doesn't change anything.
jshell imports a lot of classes on startup and any subsequent snippet (even if load from file) can use them, SO few of the missing imports can be taken care here.
--no-startup can be used to disable this behavior.
This problem seems to be related to the „forward references“ feature of JShell: When a method or type declaration refers to a still undefined variable or method, JShell checks only for syntactical correctness and delays the compilation of the method resp. type. In interactive mode it prints a helpful message like „method x refers to undefined method y and can’t be invoked until method y is defined“. But while a startup script runs, that message is not printed and in many cases the script executes nothing, which is very confusing. I have not found a way to enable the printing of that message.
Hypercritical persons might say that this bug/feature renders JShell useless for „real“ scripting applications, and I tend to agree.
I propose that the „forward references“ feature be disabled during the execution of scripts. In order to make forward references still possible, the entire script should be considered ONE big snippet. That would also eliminate the problem with line-numbers-per-snippet which makes no sense at all with code parsed from scripts.

How does the IDE verifies the syntax errors simultaneously on the go?

I am new to the Programming era so this question might sound weird to some BUT here is my doubt :
When i write a program using a text-editor then while compiling the code I used to get syntax errors like forgetting to put the semicolon at the end of statement or forgetting to put the closing curly braces( }, etc) and some other reasons like range for numeric types and all....
But when I am writing a piece of code in my Eclipse IDE, then it generates the errors spontaneously, if any, while I'm writing the code.
My question is how does the IDE's performs this operation and what kind of mechanism(datastructures, etc) it uses to accomplish this task ?
Each IDE uses its own way. Specifically eclipse uses its own "patched" compiler instead of javac. This compiler is invoked by IDE once you are saving the file (by default), so it compiles files while you are typing.
The eclipse compiler has unique features like ability to compile code with some syntax errors. It actually compile what it can and throws exception if you arrive to not compiled code during your execution. It is very convenient during development.

Why would a program silently fail?

I have a legacy java app written up by a previous developer. A bug has recently been found in it, and I've been given the task of fixing it.
Part of the problem is that there were never any error messages reporting from it.
By putting in a lot of logging messages, I finally narrowed it down to a specific line in the code - it's trying to run a method on a null object.
This is something that SHOULD have thrown an error into the log. Yet it hasn't. And even fixing this one, there's a lot of this problem in the code - assuming something will have a value when it doesn't. Every time I put in a data-verification for one, it fails somewhere shortly down the line for a very similar reason, and then I have to go through the hassle of putting in logging commands every other line again to finally narrow it down.
Why would a java program be silently failing instead of throwing errors? I can't seem to find any sort of setting suggesting that this is on purpose, but I'm really not even sure where to look for such a thing.

Ignore exceptions when executing bytecode (java)?

I have a large program, that i modified in java. I used the intelliJ idea (community edition) IDE for compiling. When i go to run the program, it starts up the GUI and then proceeds to do everthing i want from it, with very few problems (of which are unrelated to the exceptions). But the code always generates class not found exceptions (even the original unmodified code does this once you extract it from the .jar file. Despite these errors, it executes within the IDE perfectly, while still noting the errors, but they don't appear to have an effect on the program. However, when i execute them from within the virtual machine (with java filename) the exceptions which are usually ignored prevent the ultimate execution of the program. The errors are exactly the same as the ones that the iDE shows, but the IDE ignores them! How could i get a virtual machine to ignore the errors and execute the program (is there an option to pass to java - for example java -ignoreerrors filename).
Is this possible, or will i have to alter the code?
There's no way to ignore ClassNotFoundExceptions unless that class isn't actually needed by the code. Some frameworks do that by trying to load a class to discover whether some feature is available. However, if a CNFE is preventing your app from running, you'll just have to fix it. If you show some stack traces, someone might be able to steer you in the right direction.
If you are having trouble with ClassNotFoundExceptions then you can always localize the problem and catch and log using try { ... } catch (...) { ... }.
If you are instead getting ClassNotFoundErrors then it's not a localizable problem with reflection, but a failure to initialize code that's needed. You should try to prune unneeded dependencies but you really shouldn't use classes that haven't initialized properly.
If you absolutely have to, you can always load your program using a custom ClassLoader that generates bogus empty classes for any name that is not resolvable using the system classloader and use that to load your main class. That will replicate, to some degree, what your IDE is doing, though your IDE probably goes the extra step to make sure that partially well-defined classes have the right interface even if some methods are stubbed out because their bodies don't compile.
You can only ignore compiler warnings. You cannot ignore errors.
The errors that IntelliJ shows are coming from the same compiler.
ClassNotFoundException would indicate that your code failed to dynamically load a class at runtime.
This could mean that a required dependency (jar) is missing from your classpath. Try to consult your code documentation and make sure you've resolved all runtime dependencies. Also make sure that the dependent jars are in the classpath otherwise the runtime won't be able to find them.

Stand-alone Bytecode Verifier

In my bytecode instrumentation project, I stumble frequently on VerifyErrors. However, the default java Verifier gives little information on which instruction resulted in the error (it only gives the method and a small message). Is there any stand-alone bytecode verifier which provides with a little more advanced help in locating the error, at least the precise instruction location? Thank you.
ASM CheckClassAdaptor.verify() gives great feedback:
http://asm.ow2.org/
I was also looking for something that would report potential verify errors, but especially IncompatibleClassChangeErrors. I wrote a little test project with one API class and another client class calling API methods, plus a main class to run a verifier; then changed the API, recompiling it but not the client, and checked to see what could be caught. Used -target 7 though no special JDK 7 features for now.
First and most obviously, Class.forName can find certain errors in the client class's signature, but it does not seem to check method bodies for calls to nonexistent API methods and the like, even if you call getDeclaredMethods; the errors are reported by the VM only when the problematic line of code is actually run.
JustIce in BCEL 5.2 seems to be easiest;
org.apache.bcel.verifier.Verifier.main(new String[] {clazz});
does the job:
Pass 3a, method number 1 ['public void m()']:
VERIFIED_REJECTED
Instruction invokestatic[184](3) 4 constraint violated:
Referenced method 'x' with expected signature '()V' not found in class 'API'.
....
I tried ASM 4.0, but
org.objectweb.asm.util.CheckClassAdapter.main(new String[] {clazz});
does not work; perhaps it checks the format of methods, but not linkage. Inlining main and passing checkDataFlow=true does not help.
Searching, I also found https://kenai.com/hg/maxine~maxine/file/8429d3ebc036/com.oracle.max.vm/test/test/com/sun/max/vm/verifier/CommandLineVerifier.java but I could not find any way to make this work; the accompanying unit test throws a ClassNotFoundException when run.
As with any project involving JVM bytecode, I would first check to see whether the BCEL has anything that might be useful for you. Also, perhaps FindBugs may help - though I'm not sure whether it assumes verifiable bytecode to start with or not.

Categories

Resources