How can I write and run java programs in Ubuntu 11.10 ?

Asked by Prasad Chippada on 2011-11-11

Earlier I was learning Java in windows environment Now I am new to Ubuntu. How can I write Java programs in Ubuntu OS ? If I write a program in gedit then, how to save it and run as a java program ? Pl provide me some easy steps to continue my learning of the language.

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu openjdk-7 Edit question
Assignee:
No assignee Edit question
Last query:
2011-11-11
Last reply:
2011-11-12
Prasad Chippada (p-chippada) said : #1

and my Laser Jet 1000 Printer doesn't work on Ubuntu. whenever it automatically downloads the required plug in it gives some error message. How can I solve this problem too ?

Jacobsallan (jacobsallan) said : #2

Start by downloading Java. Use the Software Center and download OpenJDK Java 6 Runtime.

Make sure that java is in your search path. Type

% which java
% java -version

and you should get meaningful responses instead of error messages. If that does not work, try

% /usr/lib/jvm/java-6-openjdk/bin/java -version

You will want to update the symbol PATH. If you need help, request it.

gedit is fine for a start. Here is a sample program. Save it in a file EchoArgs.java in your home directory.

package echoecho;
public class EchoArgs {
public static void main(String[] args) {
for (int i=0; i<args.length; i++) { System.out.println(args[i]); }
}
}

Use the command line. cd to your home directory. Run

% cd ${HOME}
% javac -d .

${HOME} will get substituted with your home directory. The % is not to be typed -- its a sign that this is a command line. The '.' after the -d is short for current working directory. This command line will create a subdirectory echoecho of your home directory and place a file EchoArgs.class in that subdirectory. The .class file is a compilation of your .java source code.

Next, run the code

% java -classpath ${HOME} echoecho.EchoArgs arg1 arg2 arg3

All this should be nearly identical to what you do in a Windows environment.

Please post a separate question for your printer problem.

If you're first starting out learning how to program in Java, you'd probably be best off starting with Java 7, since Java 6 will only become more and more outdated...

The OpenJDK 7 packages are named the same as the OpenJDK 6 packages except with a 7 instead of a 6.

Can you help with this problem?

Provide an answer of your own, or ask Prasad Chippada for more information if necessary.

To post a message you must log in.