How can I compile and run a Java program? - java

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.

Related

How to compile Java code without an IDE?

I am a beginning programmer that has been using an IDE to learn to code. However I want to try using a text editor to see which one I would like more. My question is, once you download a text editor, what tools do you need to compile and run a program and how do you set them up?
You just need the JDK. Installing it (it has an installer) should set it up sufficiently.
When you want to compile something, use the javac tool:
> javac MyClass.java
When you want to run something, most likely you'll want the java tool (although of course if you're doing servlets or such, you'd use something else):
> java MyClass
Or of course, go ahead and use a separate text editor if you like but continue to use the IDE to compile, not least because presumably any IDE you were using had an integrated debugger, and a debugger is an essential tool.
If you want to use a text editor for java programming don't forget to add java to your Environment Variables Path. You can check if java is already added to your path by running java -version in your command line. If you get nothing you have to add C:\Program Files\Java\jdk1.8.0; to your path (your version of the jdk may be different, change it to the name you have). Don’t forget that semi-colon ; at the end.
Now you are set and ready to compile and run java programs just as explained by #T.J. Crowder in the answer above.

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.

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).

javac in Eclipse?

Newbie on eclipse, I have a naive questions.
I have found nowhere in Eclipse to execute the equivalent command line "javac". If I understand well, "run" from the menu of Eclipse processes as javac + java. So my question is: Under Eclipse, how can we compile a .java with "javac" only ? Thanks.
For information, I notice this similar post
How to run Javac from Eclipse
does not answer my question. Mine is to "compile a .java file without running anything"
Well, you can make Eclipse run Ant for you... but normally you just get Eclipse to compile the code for you with its built-in compiler. There's no need for javac.
By default, Eclipse will build every time you save - you don't need to explicitly build at all. When you use "Run" in Eclipse, it doesn't need to compile at that point, because the code has already built.
If you want to find the class files, just look under your output directory.
Eclipse should build your projects automatically so you don't need to call javac

Java: how to compile a huge behemoth of source code

I've downloaded a big java program in raw source code. How can I compile it? If I try to compile classes with javac I only get errors.
I guess I have to use eclipse? Can anyone point me to a guide on how to compile such a large project in eclipse?
Edit: As some people pointed out, Eclipse does not use the javac compiler. Oh well, we live to learn. That does not change the rest of my answer.
You have to realize that Eclipse uses javac to compile the source code. So, if you can't do it, chances are Eclipse won't do any good either.
It's highly unlikely that all the files are part of the same package/component. You have to figure out (based on the errors), what is the packages organization and the build order.
However, I would suggest you to look around for a build.xml or a Makefile somewhere close to the top of the tree. If you fidn the former, you could try using ant (automated build tool) with it; if you found the latter, try make on it.

Categories

Resources