Ubuntu FAQ: Compiling and Running Java Programs

2014-10-08  Source: Original Site  Category:Ubuntu  Views:6 

Advertisement

Working on the rest, post suggestions as well. I am not skilled or experienced in Java

So you're willing to write your own Java programs, or simply compile someone else's code? This guide will get you started...

This FAQ assumes you're trying to compile error-free code. To actually learn the Java languages, see the How to start programming (http://ubuntuforums.org/showthread.php?t=333867) thread.

0.a. Make sure the third party repositories are enabled

The easiest way to do this is open Applications->Add/Remove and select "All Available Applications".

0.b. Make sure the sun-java6-jdk package is installed

This package installs the essential tools needed to write and run Java programs.

sudo aptitude install sun-java6-jdk

Also run this to set the Sun Java as the default.

sudo update-java-alternatives --set java-6-sun

1. Compiling your first Java program

Copy & paste this code into a new file. Save the file as Main.java

//First Java Program
class Main
{
public static void main (String[] args)
{
System.out.println ("Hello, world.");
}
}

Open a terminal, go to the directory where you saved Main.java, and type:
javac Main.javaIf all went fine, nothing is printed and you get back to the shell. Now, run it:
java Main"Hello World!" gets printed in the terminal.

Explanations: javac is the Java compiler
obviously, Main means that the compiler must compile this file.
java Main runs the newly compiled executable.

2. Paranoid programming is good for your health!

Many beginners want to avoid compiler errors and warnings at any cost, which often leads them to ignore the "cryptic" messages the compiler outputs and to tinker anxiously with the relevant line until the error goes away.
Don't fall in that trap! The compiler is your friend, the messages it outputs are kind advices rather than punishments. You should really take the time to try and understand those messages, this is a very good way to improve your code's quality.

As a matter of fact, most seasoned programmers set the compiler's warning level as high as possible, so that it catches the little errors they make when they aren't paying much attention.

**Help needed** Post in thread to recommend content or deletion of this section.

3. Useful tips

**Help needed** Post in thread to recommend content or deletion of this section.

4. Read the manual!

Plain old manpage:
man java

Naturally, the Documentation and Tutorial on the Sun site is a great place to get answers: http://java.sun.com/docs/books/tutorial/

Thanks to so many people @UF for their very useful help, this FAQ wouldn't be the same without them. In particular, this thread is based on aks44's thread on C and C++ (see link in sticky). Not only is this thread based on it, it is blatently copying it, I hope he doesn't mind...

Related articles
  • Ubuntu FAQ: Compiling and Running Java Programs 2014-10-08

    Working on the rest, post suggestions as well. I am not skilled or experienced in Java So you're willing to write your own Java programs, or simply compile someone else's code? This guide will get you started... This FAQ assumes you're trying to comp

  • Ubuntu FAQ: Compiling and Running C# Programs 2013-01-08

    Working on the rest, post suggestions as well. I am not skilled or experienced in C# So you're willing to write your own C# programs, or simply compile someone else's code? This guide will get you started... This FAQ assumes you're trying to compile

  • Ubuntu how to set gedit for compiling and running java programs 2012-03-20

    hi, i m newbie here....and i m using ubuntu 9.04. can anyone tell me tht how can i set gedit for compiling and running java programs....i have some threads but not anyone working for me....i very begginner here so plz tell me step by step info to set

  • Ubuntu set gedit for compiling and running java programs 2015-02-09

    hi, i m newbie...can anyone tell me how can i set gedit for compiling and running java programs.. thnx in advance

  • Compile and run java program by java 2012-11-27

    hai friends, How can i comiple and run java program using java code(with out using batch file) plz any one help me in t \his.

  • Ubuntu Beginner compiling and running c++ program 2012-12-09

    Hello I am new to linux. I want to compile and run a simple c++ hello word program, I use the following command g++ hello.cpp -o ./hello but it doesn't work. I've also tried to run the executable separately but it keeps saying bash: ./hello: Permissi

  • Ubuntu HOWTO: Set gedit to compile and run Java source code 2013-03-18

    Introduction I use gedit with the terminal panel plugin when I'm programming small Java programs. This becomes a hassle sometimes, so I created a simple gedit "Tool" that would compile and run Java source files. Some people would prefer using just th

  • Does GCC compile and run Java source code 2012-01-07

    I need only to ask, if GCC can be used to compile and run Java source code and if it's only can be used for compiling Java so how can i run its code under mandrake..

  • compiling and running java applications 2013-08-25

    hey..so i downloaded jdk 1.5 on my kubuntu..now i compile and run java apps normally on this OS simulator called OSP2..anyway its manual says that i have to set a PATH variable for bash by placing the following in the .bashrc file in the home directo

  • Compiling and running java source (using vim "map") 2013-11-30

    I've started using vim recently as my main text editor and I've tried to come up with a map command which compiles and runs my java source code. This is what I have so far: map <F4> :!javac % <CR>:!java %< <CR> but it doesn't alw

  • How to compile and run Java codes in ubuntu 8.10 2012-08-21

    I am new to linux and would like to know how to compile and run the java codes on linux. Currently I have installed Ubuntu 8.10 so kindly help me to know what are the different packages required to perform the respective job and how can I continue my

  • Ubuntu Compile and run JAVA in gedit 2014-07-04

    I'm a beginning java programmer (in two days I have my exam) and I use eclipse by default as editor and compiler. However I want to compile and run my java programs also in gedit, because I love the simplicity of gedit and for a fast check, gedit is

  • Ubuntu C++ Compiling and Running 2014-05-14

    I'm new to Ubuntu and also am just beginning to learn how to program. This afternoon I tried to set up a simple program to compile and run through the terminal, but it just gives me a > prompt that won't do anything after that. I downloaded/instal

  • compiling and running C programs 2014-05-12

    i am new to C programming and i need a way to compile and run my code.I've made a little research and i found that eclipse is a nice IDE.But i don't know how to configure it and generally... what do i need to download and install to make it work.I've

  • Ubuntu I am unable to compile and my java programs 2013-10-04

    i just started using ubuntu 8.04 and i have installed netbeans. the problem is that anytime i try to compile my java programs with javac from the terminal i keep seeing this [email protected]:~$ javac Welcome.java javac: file not found: Welcome.java U

  • How to compile and run java source from java program? 2015-02-28

    Any idea of how to compile a java source and run the code from java program. i am planning to write an IDE and i badly want to know how to do this? Any tips or help on writing an java IDE will be greatly appreciated.

  • Ubuntu Codeblocks says Permission denied when I compile and run my program 2014-01-29

    Hi I installed Codeblocks from apt-get and I wrote a simple hello world program to test it. When I run my program a new terminal opens and says: sh: /home/david/addressoffile: Permission denied Press Enter to continue. Any idea why I keep getting it?

  • Ubuntu Using KDevelop to compile and run C++ programs 2015-03-24

    So, I am having a problem getting KDevelop to compile my program: #include <iostream> using namespace std; int main () { cout << "Hello, World!" << endl; return 0; } When attempting to compile with the command cc helloworld.cpp The

  • Ubuntu C compiling and running programs 2013-01-06

    everyone, I am trying to compile C programs, I used pico, saved the helloworld.c then in the terminal typed gcc helloworld.c -o helloworld I get no errors but then when I try to run it I type just helloworld and get prompted with bash: helloworld: co

  • Ubuntu How to compile and run C programs ? 2014-10-17

    I am new to Ubuntu I am having a little 'C' program with me //hello.c #include <stdio.h> void main() { printf("Hello From Ubuntu"); return ; } when I tried to compile it using "cc hello.c" I get following errors : hello.c:1:19: error: stdio.h: