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.
Related
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).
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.
Is it possible to easily manage and compile native Java classes alongside Clojure in a project using leiningen?
I am working at a pretty low level (with netty nio) and thinking that some of the plumbing classes would actually be easier to handle as raw java both in terms of constructing the code as well as performance.
As of Leiningen 2.x, :java-source-path has been replaced with :java-source-paths, whose value is now specified as a vector rather than a string.
A good place to find a full (up-to-date) documentation of Leiningen features is to peruse the sample project file. In this case, you will see:
:java-source-paths ["src/main/java"]
In Leiningen tutorial there is following statement
For projects that include some Java code, you can set the :java-source-path key in project.clj to a directory containing Java files. Then the javac compiler will run before your Clojure code is AOT-compiled, or you can run it manually with the javac task.
so it should work out of box if :java-source-paths option is set
Use Vinyasa - I wrote it especially to deal with this problem
Here is a blog post Dynamic reloading of java code in emacs/nrepl
How can I compile and run a Java program?
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html#win32-2b
Is English difficult for you? There are tutorials for your language too.
to compile:
javac path/to/file/with/main.java
then to run:
java path/to/file/with/main
I would suggest that if you are starting with the java language, first use an IDE (Eclipse, Netbeans, Intellij IDEA or another. For a first step let the IDE do his job. Then once you evolve quickly use a build manager (Ant, Maven, Gradle, ...). It is a good practice to always have a build manager in place to keep the build in control.
To compile (assuming you are in the same folder as the file is)
javac "filename.java"
To run
java "filename"
make sure you have set the environment variables correctly, so that the command line can detect where javac is.
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.