LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-16-2009, 07:49 AM   #1
vskram21
LQ Newbie
 
Registered: Oct 2009
Location: Chennai,India
Distribution: fedora,ubuntu and puppy
Posts: 23

Rep: Reputation: Disabled
How to compile and execute C++ and Java in Fedora 11 and Ubuntu 9.10?


I have installed Fedora 11 and Ubuntu 9.10 and have instaled development packages(gcc,gc++,..) in both i am able to compile and execute c programs but when it comes to c++ i get an error...
i ve tried using cc & gcc..
how can i over come this??
 
Old 11-16-2009, 08:31 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

When compiling C++, use the command "g++" instead of "gcc".
 
1 members found this post helpful.
Old 11-16-2009, 10:38 PM   #3
vskram21
LQ Newbie
 
Registered: Oct 2009
Location: Chennai,India
Distribution: fedora,ubuntu and puppy
Posts: 23

Original Poster
Rep: Reputation: Disabled
tryed c++ and g++ too still getting same errors..
i have also set executing permissions for the files..
 
Old 11-17-2009, 01:36 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Then post the error messages?
 
Old 11-18-2009, 06:32 AM   #5
vskram21
LQ Newbie
 
Registered: Oct 2009
Location: Chennai,India
Distribution: fedora,ubuntu and puppy
Posts: 23

Original Poster
Rep: Reputation: Disabled
[temp@satheesh Documents]$ vi sam.cpp
#include<iostream>
main()
{
cout<<"Welcome\n";
}
[temp@satheesh Documents]$ cc sam.cpp
sam.cpp: In function ‘int main()’:
sam.cpp:4: error: ‘cout’ was not declared in this scope

[temp@satheesh Documents]$ vi sam.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
[temp@satheesh Documents]$ ll
total 8
-rwxr-xr-x. 1 temp temp 49 2009-11-18 14:33 sam.cpp
-rwxr-xr-x. 1 temp temp 118 2009-11-18 14:46 sam.java
[temp@satheesh Documents]$ java sam.java
Exception in thread "main" java.lang.NoClassDefFoundError: sam/java
Caused by: java.lang.ClassNotFoundException: sam.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: sam.java. Program will exit.
[temp@satheesh Documents]$
 
Old 11-18-2009, 08:26 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Try

g++ sam.cpp

instead of

cc sam.cpp

You also need the line

using namespace std;

before the main function.
 
1 members found this post helpful.
Old 11-19-2009, 07:41 AM   #7
vskram21
LQ Newbie
 
Registered: Oct 2009
Location: Chennai,India
Distribution: fedora,ubuntu and puppy
Posts: 23

Original Poster
Rep: Reputation: Disabled
why should one use using namespace std;
i am able to compile it after including it..
how about java??
what shall i do??
 
Old 11-19-2009, 08:48 AM   #8
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

Well cout is part of the std library of C++. And everything in it is in the std namespace. If you don't have "using namespace std" you can use std::cout instead of cout.

As for Java, you have to compile the Javaprogram using javac. And then you need Java sdk or jdk, not just the jre. The javac compiler will make a .class file you can run.
 
1 members found this post helpful.
Old 11-19-2009, 02:54 PM   #9
armanox
Member
 
Registered: Sep 2005
Location: Baltimore, MD, USA
Distribution: Fedora, Gentoo, Debian, Slackware, IRIX, OS X
Posts: 192

Rep: Reputation: 32
First, your java program will not compile as is. The line that says
Code:
public class HelloWorld {
must match the file name - you either need to change the name to HelloWorld.java OR change the line to say
Code:
public class Sam {
.

Then you need to build the java file (assuming you have a Java Development Kit installed) using
Code:
javac Sam.java
and then run using
Code:
java Sam
.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Programming: Java Runtime Environment not found when trying to compile murbz Linux - Software 2 03-26-2009 03:04 AM
Java Installation, Fedora Core5. Cannot Execute Binary File. JordanD Linux - Newbie 2 03-03-2008 01:29 PM
Installing Java, Fedora Core5. Cannot Execute Binary File. JordanD Linux From Scratch 1 03-02-2008 01:06 PM
cant compile java files in fedora 8.. java/ maranganesan Linux - Software 4 12-31-2007 12:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration