How debugging is much easier in eclipse for java programs? - java

Whenever I write a program in eclipse each line gets complied as go to next line.It shows compilation is done while writing he program only.
Does it mean javac.exe file is run whenever you write a line of program?
It debugging is much easier in Eclipse.

Eclipse has his own compiler (JDT). It does not use the javac.exe compiler. The question is how does it achieve such short compilation times?
The internal data structure maintained by Eclipse for representing a Java program (AST) is the same data structure used by the JDT compiler. This sharing of data allows the compiler to run faster as it does not need to re-compile the whole program (or even the whole file) again.
Also, unlike javac.exe the JDT compiler resides inside Eclipse. It is not a separate process so it does not need to be loaded (by the operating system) the same way javac.exe does.
In addition to that (and this is true to all Java compilers), Java has dynamic linking. Each class is linked into the program when it is loaded during the execution of the program. This obviates the need for a linking phase at the end of compilation (a-la C/C++/C#). The linking phase is generally quite long as it processes the program as a whole (as opposed to compilation which is carried out on a a file-by-file basis). Thus, linking gets slower as the program grows. Techniques such as incremental linking have managed to mitigate this slow-down but not entirely.
Given that Java needs no (static) linking, you can get to a state where a Java program is ready to run much faster than in other (statically linked) languages.

Eclipse actually has it's own compiler that is compiling your code as you type. It's not javac.exe but you can read more about it at the JDT page.

I believe the Eclipse IDE uses its own Java compiler.
Javac.exe is Sun's own official Java compiler, which I'm sure the Eclipse compiler borrows heavily from.

You can turn it off in Project->Build Automatically. Eclipse is using it's own java compiler - eclipsec instead of javac.

Related

Input redirection with Java via windows command line

I have recently started the Princeton algorithms course (available on coursera) but I’m running into issues with the choice of an IDE and input redirection. Princeton recommends using their Dr Java IDE, but this does not support input redirection, which is required pretty regularly. I can still write and compile programs in that IDE, and then run them from the command line (I’m using windows), which is the route the authors appear to recommend. However all of their programs make use of their StdLib.jar file. Compiling the program is fine (the IDE settings can reference the necessary JAR) but it seems that each time I want to run a program with input redirection (which is VERY common in the book and course) I would need to type java -cp .;algs4.jar calcAverage < input.txt. This seems like unnecessary repetition, and the book is loaded with command line examples that would simply say java calcAverage < input.txt. Is there not a way to avoid constant setting of the class path?
Note, I'm aware that I could potentially use another IDE, but what bothers me is that I suspect I've missunderstood something.

Can we combine the "javac" and "java" commands

The Groovy command-line command "groovy" appears to compile a Groovy source file (.groovy) to a .class file and then run it (if it's a script).
Is there a good reason why there is no equivalent command to both compile and run a .java file?*
Why do we need to do two steps?
javac XXX.java
java XXX
why not just
java XXX
?
*perhaps I am gravely mistaken and there is such a thing
Can you do it? Yes. It is simple to write a script that runs javac followed by java.
Is it a good idea? Well that's up to you to decide. But I think you would find that running javac followed by java takes significantly longer than just running java. So unless you typically run a Java program just once, it makes sense to keep the two steps separate.
So ...
Is there a good reason why there is no equivalent command to both compile and run a .java file?
Yes. Because you typically want to compile (build) and run as separate steps ... for performance reasons.
Groovy is a dynamic language. So there is no compile step. It's write->execute. When you develop an application with Groovy what you ship is the actual source you've written. Java is a static language. When you are developing something with Java you do write->compile->execute. Once you have developed the application what you ship is the compiled code. That's why there is a separate command 'javac', to compile the code and produce the artifacts that you would be shipping to the customer. Of course when developing it's much easier to run the java code directly. Most IDE's provide this. e.g: In eclipse you could just run a Java class which has the main method.
=================================
EDIT : for whoever downvoted :-)
I outlined the different intents of Groovy and Java. Groovy is meant to be a dynamic/scripting language. Therefore like many other such languages doesn't have the 'compile' step. Java is meant to be a static language. Therefore a 'compile' step is present where you do the static type checking.

playOrm using Eclipse

For starters, and granted being new to java development, I have a few questions.
I am using the Eclipse IDE, and have downloaded a zip file *com.alvazan.orm library.
Initializing a Java Project from an Existing Ant Buildfile, and using the build.xml file, I get TONS of com.alvazan.orm.api packages, each containing various .java files/test cases and and equal number of .Jar files containing even more packages, etc.
So, right off the bat, I notice several warnings, Java Build Problems
Classpath variable 'JRE_LIB' in project 'std_buildfile' is deprecated: Use the JRE System Library instead
Is this something that will effect the running and debugging of test cases?
Additionally, I have run into Java Problems, upon initial build:
The method translateToColumnImpl(Collection, RowToPersist, Collection) from the type DboColumnToManyMeta is never used locally
The value of the local variable existing is not used
The value of the local variable toBeAdded is not used
The value of the local variable toBeRemoved is not used
While these are currently only errors, since attempting to run various test cases and coming up with even more warnings and errors, I am concerned the looming warnings maybe affecting the outcome.
Please advise if this something which needs addressing or if is generally a common occurrence.
I would be glad to post more information of necessary, just let me know what is necessary.
These "warnings" not errors are a common occurence. You only have issues when you see the red errors. Eclipse likes to tell you about potential problems like generally unused fields should be deleted. Many times fields are not deleted because they are about to be used. None of the warnings ever affect your ability to run or debug the programs though. ONLY the red errors will affect your run AND when you try to run in eclipse with red warnings eclipse will even prompt you saying "this doesn't even compile, are you sure you still want to run" and generally you should not run when you have red errors.

Recompiling TJWS

I am new to java and i have to make some changes to Tiny Java Web Server(LINK) and recompile it.
Can someone please explain how it is done in java?
Usually i have been using javac command for compiling, but here i have multiple files to compile.
Also, i have read someone i should be using ant , but it seems complicated.
Why does compiling a program have to be so complicated in java?
Can i compile it somehow using eclipse?
Answering your implied question "why can't all Java programs be compiled using Eclipse" -- you see, there are tools which compile Java programs consisting of many files (not just one), and these tools perform compilation automatically, without human operator, without using Eclipse (or any other IDE), so they need a compilation program. One such program is Ant, another popular in Java world is Maven.
Another point for Ant is that often you need just to compile program, not to edit it in anyway, so you don't need IDE, only compilation tool.
If you interested in understanding how these compilation tools work, I would suggest starting with Ant, then proceed to Maven (if needed).

Compiling Java file with code from within a Java file

I'm currently creating a personal (maybe public) java terminal. I want to create a command that will create + compile a Java file on execution, except I'm not too sure on how to actually do this. Is it possible? Or am I just dreaming?
You could also use Groovy - it's quite handy if you just want to compile and run a line or two of Java code from within your application. The application may be in regular Java, with Groovy used only for compilation of the dynamically generated code. Whichever solution you choose, be careful, as executing user input as code can lead to security issues (vulnerability to injection attacks).
compile a Java file
See the STBC. It uses the JavaCompiler to compile the code in the text area.
I agree with #eee's comment that javax.script is probably a very nice fit for your project, script code is easier to deal with than Java code. I've successfully used it in the past for a plugin API, I don't remember having had any problems to get it up and running.
Most projects that I know of that compile real Java at runtime use the Eclipse compiler to do so. The Java 6 javac can be accessed completely programmatically as well. I've never used either of these myself. These two and some other compilers can be accessed via Commons-JCI if desired.

Categories

Resources