First Java Program

Questions about programming languages and debugging
Dltr
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 210
Joined: 28 Oct 2009, 17:00
14
Location: Germany
Contact:

First Java Program

Post by Dltr »

Well , Here we go
I am using Ubuntu and as a matter of fact it is not that easy to run this small example(First Example in Java) in Command Prombt which means in Ubuntu or generally in Linux the Terminal(Console) and really dont know if this bold Information is right or not !!

Code: Select all

class apples{
    public static void main(String args[]){
	     System.out.println("Hello youtube!");
	}	 
}
So briefly i wrote this Example in the Notepad++ and i would like to run it , so what are the next steps please :oops: , where should i go then and what should i do :roll: ?

Note I am now in Progress to learn Java Programming (as a Noob) so i wanna apologize for this silly Question :lol: !


Thank You
Dltr

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

You need to make sure that you have the Java JDK, to compile the code, then you need the JRE to run it =)

In Ubuntu you would do the following to get that
sudo apt-get install sun-java6-jdk
sudo apt-get install sun-java6-jre
Then you simply use javac to compile and java to run
javac nameOfFile.java

java nameOfFile

Good luck :-)
"The best place to hide a tree, is in a forest"

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

Code: Select all

sudo apt-get install sun-java6-jdk sun-java6-jre
:P

Also note that the name of the file has to be the same as the class that is in it (with a .java extension), and class names should start with a capital letter.

So you would do:

Code: Select all

class Apples {
    public static void main(String[] args) {
        System.out.println("Hello youtube!");
    }
}
And put that in Apples.java

Then compile with

Code: Select all

javac Apples.java
java Apples
By the way, you misplaced the brackets in the arguments of main. I corrected this in the code above.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

leetnigga wrote: By the way, you misplaced the brackets in the arguments of main. I corrected this in the code above.

It's odd that the compiler didn't react to that, it's usually very sensitive.
"The best place to hide a tree, is in a forest"

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

cats wrote:It's odd that the compiler didn't react to that, it's usually very sensitive.
Apparently (I just found out) Java also allows placement of the array brackets with the variable name. This is highly discouraged.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

leetnigga wrote: Apparently (I just found out) Java also allows placement of the array brackets with the variable name. This is highly discouraged.
Figures
"The best place to hide a tree, is in a forest"

Dltr
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 210
Joined: 28 Oct 2009, 17:00
14
Location: Germany
Contact:

Post by Dltr »

Thank You Guys for Replying ...

Well i tried to do what you worte

first
javac Apples.java and the i became this here :

Code: Select all

javac Apples.java
javac: file not found: Apples.java
Usage: javac <options> <source files>
use -help for a list of possible options
and also this for java Apples

Code: Select all

 java Aplles
Exception in thread "main" java.lang.NoClassDefFoundError: Aplles
Caused by: java.lang.ClassNotFoundException: Aplles
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Aplles.  Program will exit.
Don't really know what is going on Here :oops:

and Now the Other deep Problem i am facing is that i can't install the Eclipse IDE for Java EE Developers as a specifically Editor for Java !
-I found an Eclipse on my Ubuntu Software Center and dont know if that is the right Editor or not !:roll:

Waiting for Heroes to Help ... :?

Thank You
Dltr

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

You have to change the current directory to the directory containing your file first.

If you saved your file in /home/leetnigga/Apples.java you would do:

Code: Select all

cd /home/leetnigga
Also, you misspelled Apples when you tried to run the program.

Dltr
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 210
Joined: 28 Oct 2009, 17:00
14
Location: Germany
Contact:

Post by Dltr »

leetnigga You are very patient ! :D

well look,can you Explain in Steps because i really want to get that ... here are the steps that i did :

-To write My First Program,First i downloaded the Notepad++, i could use the gedit text editor for writing ,but You know I am still a Noob in Java and i should follow the Steps :lol: ...
-Then i saved what i wrote in Notepad++ on the Desktop with the name for Example jamesbond.java and Now what i wanna do is to run what i wrote and saved in the Desktop in the CommandPrombt(Terminal-Console)! ...
-Then i did what Cats wrote me
sudo apt-get install sun-java6-jdk
sudo apt-get install sun-java6-jre
-Then I get this here :
Reading package lists... Done
Building dependency tree
Reading state information... Done
sun-java6-jdk is already the newest version.
The following packages were automatically installed and are no longer required:
linphone-common linux-headers-2.6.31-14 libosip2-4 libboost-regex1.38.0
libccgnu2-1.7-0 libopal3.6.4 libpt2.6.4-plugins linphone-nox libpt2.6.4
libexosip2-4 liblinphone3 libmediastreamer0 libqt3-mt libccrtp1-1.7-0
linux-headers-2.6.31-14-generic libreadline5 libzrtpcpp-1.4-0 libortp8
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
-Then i tried to use

Code: Select all

javac Apples.java
java Apples
but then i get Errors but not for Example Hallo Youtube!

Waiting for Reply..

Thank You
Dltr

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

The apt-get command only had to be run once. Running it again simply tells you "0 upgraded, 0 newly installed", so it had no effect.

If you saved your file on the desktop (I think I recall that being the Desktop folder in your home directory), after you opened a terminal you would have to do this:

Code: Select all

cd Desktop
Now you can compile any files that you saved there with

Code: Select all

javac <filename>
And run them with

Code: Select all

java <filename-.java>
Like you have been doing all along, except now you would be doing it in the right directory.

User avatar
Big-E
Administrator
Administrator
Posts: 1332
Joined: 16 May 2007, 16:00
16
Location: IN UR ____ , ____ING UR _____ .
Contact:

Post by Big-E »

Dltr wrote:

Code: Select all

 java Aplles
Exception in thread "main" java.lang.NoClassDefFoundError: Aplles
Caused by: java.lang.ClassNotFoundException: Aplles
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: Aplles.  Program will exit.
The answer is all in the error. Particularly, the Aplles - you named your class wrong. Fix that, and you will see it compile correctly.

Dltr
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 210
Joined: 28 Oct 2009, 17:00
14
Location: Germany
Contact:

Post by Dltr »

Code: Select all

dltr-clone@Dltr:~$ cd Desktop
dltr-clone@Dltr:~/Desktop$ javac Apples.java
dltr-clone@Dltr:~/Desktop$ java Apples
Hello Youtube!
You Guys are Brilliant .... :lol: !
And as a matter of fact when i tried what you have written at last leetnigga,i found that i made to mistakes :
- public statuc void main instead of public static void main
-System.out.printIn instead of System.out.println
those were the Errors i found through the Terminal !
thank you very much guys , you really helped


Thank You
Dltr

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

Congratulations!

Good luck with Java.

Dltr
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 210
Joined: 28 Oct 2009, 17:00
14
Location: Germany
Contact:

Post by Dltr »

Thank You Leetnigga

and One more Thing,recently i found that working with NetBeans IDE
is easier than Eclipse SDK on Ubuntu ..
*I mean with Openning a New Project then in Source Openning a New Class after that type what you and then Run it .

do you think is that right,Or maybe i should work with Eclipse and for that learn How to Use it First ?

Thank You
Dltr

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Post by Lundis »

Netbeans and Eclipse are similar. I used Eclipse at first, but switched to Netbeans.. Tbh I can't remember why anymore. Anyway, if you learn how to use one, the other won't be much of a problem to learn if you find the need for it.

Post Reply