How to Compile and Run Java Program Online
If you are learning Java, or practicing for coding interviews, you need a fast way to run many short programs.
Codiva Online IDE is best suited for your usecase. No installation needed. Works on Chromebooks and even on mobile.
If you are learning Java, there are 3 options.
- Install JDK and use text editor
- Install an IDE like Eclipse, IntelliJ
- Use Online Compilers like Codiva.
Each of these have different pros and cons.
Use Online Compilers like Codiva.
This is the easiest option if you are getting started.
- Open https://www.codiva.io on your browser.
- Create a new project, by giving a name “my first project”, and click ‘Create’ button
- Define your new Java file, by giving a name, say ‘HelloWorld’ on the new file form.
Codiva will autocreate a basic file, to get started.
class HelloWorld { public static void main(String[] args) { System.out.println("Hello Codiva"); } }
Start editing.
- Compilation happens in the background.
- You can see the compilation errors in the editor itself
Once done, and no compilation errors, click ‘Run’
Advantages:
- You can start coding in minutes. No Downloads. No installation. No path configuration.
- No context switching time between text editor and the console. Everything in one place.
- Very simple development environment. Just suited for the writing simple programs for learning.
- Works on any browser on any device. Even on low end laptops, Chromebooks or even mobile phones.
- Very easy to share code with friends or stack overflow to ask questions.
- Easy to embed in your website or blog.
Disadvantages:
- Codiva needs internet connection to work properly. Don’t worry, if you can download a facebook profile picture, codiva will work for you.
- Limited set of features. Codiva achieves its simplicity by cutting down some features. So, once you want to use advanced programs like Swing or Servlets or Android, you might start using another option.
Install JDK and use text editor
This is the basic approach.
- Download Java 9 and install. You may have to follow specific instruction depending on the Operation System.
- Use any text editor to create a file, say
HelloWorld.java
On the terminal, go to that directory, and run
javac HelloWorld.java
If there are no compilation errors, this will create a file
HelloWorld.class
(or whatever class that was in the file)Then run,
java HelloWorld
Advantages:
- Very basic tools.
- Complete flexibility, and pretty much any kind of development can be done with just the JDK and a text editor.
Disadvantages:
- Slow development because of context switching. If you are a beginner, most of the code you write is going to have compilation errors. Now, you have to switch between console and text editor frequently. You have to remember the line number and the error text, for each error and go to the editor to fix the code, and repeat the process.
- Reading through the installation steps is a turn down.
Install an IDE like Eclipse, IntelliJ
When you have a decent powered laptop or a desktop, installing an IDE is not a bad option. If you don’t have any preference, just use Eclipse.
- Install latest version of eclipse
- Once installed, click on the icon to start Eclipse.
- Create a new workspace
- Create a new Java project
- Create a new Java class, name it say ‘HelloWorld’. This will create a HelloWorld.java with a simple boilerplate. Add necessary code.
- You will see that, the compilation happens as you type. When you see compilation errors, there will be a red x on the left side.
- If there are no errors, just click ‘Run’
Advantages:
- Very powerful. You can develop very fast.
- No context switching time between text editor and the console. Everything in one place.
- Very versatile development environment. Only minor changes will be required when you start developing any complex applications.
Disadvantages:
- System requirements are huge. If you have a low end Windows laptop, it won’t work well.
- No way to work on Chromebooks or mobile phones.
- You should have installation permissions on your device. Many companies disallow installation on the work computers.
- Each time to start and stop the IDE itself will take a couple of minutes.
Happy Coding!