C C++ and Java Program in Linux and Lamp Server Installation

C C++ and Java Program in Linux and Lamp Server Installation

7.How to install GCC (C/C++) Compiler in Linux Create a sample C program then compile and execute the code.
bz@PC:~$ sudo apt install gcc
bz@PC:~$ sudo apt install build-essential
bz@PC:~$ gcc –version
C Program
Example-1:
Write the following code using any text editor and save the file with the extension ‘.c’.
#include <stdio.h>
int main()
{
printf(“Learning C in Linux Kernal”);
}
To compile and execute the code. The source file name is first.c and executable filename is first here.
$ gcc first.c -o first
$ ./first
Learning C in Linux Kernal
Example-2:
#include <stdio.h>
int main()
{
char name[100];
int age;
printf(“Enter your Name: “);
scanf(“%s”,name);
printf(“Enter your Age: “);
scanf(“%d”,&age);
printf(“Hello, %s,You are %d years old”, name, age);
}
Example-3:
#include <stdio.h>
int main(int argc,char* argv[]){
printf(“Total number of Arguments = %d\n”,argc);
printf(“Argument No. 1 = %s\n”,argv[0]);
printf(“Argument No. 2 = %s\n”,argv[1]);
printf(“Argument No. 3 = %s\n”,argv[2]);
}
8. Create a sample C++ program then compile and execute the code.
Example-1:
Write the following code using any text editor and save the file with the extension ‘.cpp’
bz@PC:~$ sudo nano hello.cpp

#include <iostream>
using namespace std;
int main()
{
cout<<“Hello World!” <<endl;
return 0;
}

bz@PC:~$ g++ hello.cpp
user@PC:~$ ./a.out
Hello World!
bz@PC:~$ g++ hello.cpp -o hello
bz@PC:~$./hello
Hello World!
Example-2:
#include <bits/stdc++.h>
using namespace std;
class Gyancs
{
// Access specifier
public:
// Data Members
string csname;
// Member Functions()
void printname()
{
cout<< “Csname is: “<<csname;
}
};
int main() {
// Declare an object of class Gyancs
Gyancs obj1;
// accessing data member
obj1.csname = “Open Source”;
// accessing member function
obj1.printname();
return0;
}
Output:
Csname is: Open Source

9. How to install Java in Linux Creating a sample java Program.

bz@PC:~$ sudo apt install openjdk-8-jdk
bz@PC:~$ sudo nano /etc/environment

JAVA_HOME=”/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin”

Java Programming in Linux
Example-1:
Write the following code using any text editor and save the file with the extension ‘.java’
bz@PC:~$ sudo nano Simple.java
class Simple{
public static void main(String args[]){
System.out.println(“Hello Java”);
}
}
bz@PC:~$ javac Simple.java
bz@PC:~$ java Simple
Hello Java
Example-2:
bz@PC:~$ sudo nano Student.java
public class Student {
public static void main(String[] args) {
int x=10;
int y=12;
if(x+y> 20)
{
System.out.println(“x+y is Greater Than 20”);
}
}
}

bz@PC:~$ javac Student.java
user@PC:~$ java Student
x+y is Greater Than 20
10.How To Install Linux, Apache, MySQL, PHP (LAMP)
Step 1: Install Apache
bz@PC:~$ sudo apt-get update
bz@PC:~$ sudo apt-get install apache2
Step 2: Install MySQL
bz@PC:~$ sudo apt-get install mysql-server
Finish up by running the MySQL set up script:
bz@PC:~$ sudo /usr/bin/mysql_secure_installation

Press y|Y for Yes, any other key for No: yes

There are three levels of password validation policy:

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
The prompt will ask you for your current root password.
Type it in.0
Enter current password for root (enter for none):

OK, successfully used password, moving on…
Step 3: Install PHP
bz@PC:~$ sudo apt-get install php
may also be useful to add php to the directory index, to serve the relevant php index files:
bz@PC:~$ sudo nano /etc/apache2/mods-enabled/dir.conf
Add index.php to the beginning of index files. The page should now look like this:
<IfModulemod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Note: Green place is add index.php, Strikethrough is remove it there.

PHP Modules
bz@PC:~$ apt-cache search php
Step 4: RESULTS — See PHP on your Server
To set this up, first create a new file:
bz@PC:~$ sudo nano /var/www/html/info.php
Add in the following line:
<?php
phpinfo();
?>
Then Save and Exit.
Restart apache so that all of the changes take effect:
bz@PC:~$ sudo service apache2 restart
How to Find your Server’s IP address
bz@PC:~$ sudo ifconfig
The address you want to visit will be:
http://your_server_IP_address/info.php

http://192.168.7.81/
http://192.168.7.81/info.php

Start the Command-line tool MySql and select a database:

bz@PC:~$sudo mysql -u root -p
[sudo] password for user:
Enter password:
mysql>

or

bz@PC:~$sudo mysql -u root
[sudo] password for user:

mysql>SHOW DATABASES;
mysql>use mysql;
mysql>show tables;

To create and populate the example table, use these statements:
mysql>CREATE TABLE item (
article INT UNSIGNED DEFAULT’0000NOT NULL,
dealer CHAR(20)DEFAULT”NOT NULL,
price DECIMAL(16,2)DEFAULT’0.00NOT NULL,
PRIMARYKEY(article, dealer));
mysql>DESCRIBE item;
mysql>INSERT INTO item VALUES
(1,’A’,3.45),(1,’B’,3.99),(2,’A’,10.99),(3,’B’,1.45),
(3,’C’,1.69),(3,’D’,1.25),(4,’D’,19.95);

After issuing the statements, the table should have the following contents:

mysql>SELECT*FROM item ORDERBY article;

+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|       1 | A      |  3.45 |
|       1 | B      |  3.99 |
|       2 | A      | 10.99 |
|       3 | B      |  1.45 |
|       3 | C      |  1.69 |
|       3 | D      |  1.25 |
|       4 | D      | 19.95 |
+---------+--------+-------+

Theory Question:

  1. What is the difference between Linux and Unix?
  2. What are the Features of the Linux Operating System?
  3. What is Linux Kernel?
  4. How many types of Shells are there in Linux? explain.
  5. Explain File Permissions types in Linux?
  6. What are the symbolic links?
  7. Explain about chmod command?
  8. Where is password file located in Linux and how can you improve the security of password file?
  9. How do you create a new user account and set the password for a user from a shell prompt in Linux?
  10. What are the different types of Kernels? Explain


Open Source Software Lab Manual


About me