Pages

Friday, December 21, 2012

Working with Manifest Files: The Basics



Working with Manifest Files: The Basics

JAR files support a wide range of functionality, including electronic signing, version control, package sealing, and others. What gives a JAR file this versatility? The answer is the JAR file's manifest.
The manifest is a special file that can contain information about the files packaged in a JAR file. By tailoring this "meta" information that the manifest contains, you enable the JAR file to serve a variety of purposes.
This lesson will explain the contents of the manifest file and show you how to work with it, with examples for the basic features:


Understanding the Default Manifest

When you create a JAR file, a default manifest is created automatically. This section describes the default manifest.

Modifying a Manifest File

This section shows you the basic method of modifying a manifest file. The later sections demonstrate specific modifications you may want to make.

Setting Package Version Information

This section describes how to use the package version headers in the manifest file.


Thanks!!
Kuldeep


Setting Package Version Information



Setting Package Version Information


You may need to include package version information in a JAR file's manifest. You provide this information with the following headers in the manifest:
Headers in a manifest
HeaderDefinition
NameThe name of the specification.
Specification-TitleThe title of the specification.
Specification-VersionThe version of the specification.
Specification-VendorThe vendor of the specification.
Implementation-TitleThe title of the implementation.
Implementation-VersionThe build number of the implementation.
Implementation-VendorThe vendor of the implementation.
One set of such headers can be assigned to each package. The versioning headers should appear directly beneath the Name header for the package. This example shows all the versioning headers:
Name: java/util/
Specification-Title: Java Utility Classes
Specification-Version: 1.2
Specification-Vendor: Example Tech, Inc.
Implementation-Title: java.util
Implementation-Version: build57
Implementation-Vendor: Example Tech, Inc.

An Example

We want to include the headers in the example above in the manifest of MyJar.jar.
We first create a text file named Manifest.txt with the following contents:
Name: java/util/
Specification-Title: Java Utility Classes
Specification-Version: 1.2
Specification-Vendor: Example Tech, Inc.
Implementation-Title: java.util 
Implementation-Version: build57
Implementation-Vendor: Example Tech, Inc.

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

We then create a JAR file named MyJar.jar by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Name: java/util/
Specification-Title: Java Utility Classes
Specification-Version: 1.2
Specification-Vendor: Example Tech, Inc.
Implementation-Title: java.util 
Implementation-Version: build57
Implementation-Vendor: Example Tech, Inc.


Thanks!!

Understanding the Default Manifest



Understanding the Default Manifest


When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest file in an archive, and it always has the pathname
META-INF/MANIFEST.MF
When you create a JAR file, the default manifest file simply contains the following:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
These lines show that a manifest's entries take the form of "header: value" pairs. The name of a header is separated from its value by a colon. The default manifest conforms to version 1.0 of the manifest specification and was created by the 1.6.0 version of the JDK.
The manifest can also contain information about the other files that are packaged in the archive. Exactly what file information should be recorded in the manifest depends on how you intend to use the JAR file. The default manifest makes no assumptions about what information it should record about other files.
Digest information is not included in the default manifest.

Thanks!!

Modifying a Manifest File(JAR)



Modifying a Manifest File within JAR :


You use the m command-line option to add custom information to the manifest during creation of a JAR file. This section describes the m option.
The Jar tool automatically puts a default manifest with the pathname META-INF/MANIFEST.MF into any JAR file you create. You can enable special JAR file functionality, such as package sealing, by modifying the default manifest. Typically, modifying the default manifest involves adding special-purpose headers to the manifest that allow the JAR file to perform a particular desired function.
To modify the manifest, you must first prepare a text file containing the information you wish to add to the manifest. You then use the Jar tool's m option to add the information in your file to the manifest.

Warning: The text file from which you are creating the manifest must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
The basic command has this format:
jar cfm jar-file manifest-addition input-file(s)
Let's look at the options and arguments used in this command:
  • The c option indicates that you want to create a JAR file.
  • The m option indicates that you want to merge information from an existing file into the manifest file of the JAR file you're creating.
  • The f option indicates that you want the output to go to a file (the JAR file you're creating) rather than to standard output.
  • manifest-addition is the name (or path and name) of the existing text file whose contents you want to add to the contents of JAR file's manifest.
  • jar-file is the name that you want the resulting JAR file to have.
  • The input-file(s) argument is a space-separated list of one or more files that you want to be placed in your JAR file.
The m and f options must be in the same order as the corresponding arguments.

Note: The contents of the manifest must be encoded in UTF8.
The remaining sections of this lesson demonstrate specific modifications you may want to make to the manifest file.



Thanks!!
Kuldeep

JAR(Java Archive) Command in *nix


JAR (Java ARchive) is an archive file format typically used to aggregate many Java class files and associated metadata and resources (text, images and so on) into one file to distribute application software or libraries on the Java platform.
              JAR files are built on the ZIP file format and have the .jar file extension. Computer users can create or extract JAR files using the jar command that comes with a JDK. They can also usezip tools to do so; however, the order of entries in the zip file headers is important when compressing, as the manifest often needs to be first.

Creating a JAR File

The basic format of the command for creating a JAR file is:
jar cf jar-file input-file(s)
The options and arguments used in this command are:
  • The c option indicates that you want to create a JAR file.
  • The f option indicates that you want the output to go to a file rather than to stdout.
  • jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.
  • The input-file(s) argument is a space-separated list of one or more files that you want to include in your JAR file. The input-file(s) argument can contain the wildcard * symbol. If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.
The c and f options can appear in either order, but there must not be any space between them.
The m and f options must be in the same order as the corresponding arguments.


This command will generate a compressed JAR file and place it in the current directory. The command will also generate a default manifest file for the JAR archive.

Note: The metadata in the JAR file, such as the entry names, comments, and contents of the manifest, must be encoded in UTF8.

You can add any of these additional options to the cf options of the basic command:
jar command options
OptionDescription
vProduces verbose output on stdout while the JAR file is being built. The verbose output tells you the name of each file as it's added to the JAR file.
0 (zero)Indicates that you don't want the JAR file to be compressed.
MIndicates that the default manifest file should not be produced.
mUsed to include manifest information from an existing manifest file. The format for using this option is:
jar cmf existing-manifest jar-file input-file(s)
See Modifying a Manifest File for more information about his option.

Warning: The manifest must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
-CTo change directories during execution of the command. See below for an example.



Note: When you create a JAR file, the time of creation is stored in the JAR file. Therefore, even if the contents of the JAR file do not change, when you create a JAR file multiple times, the resulting files are not exactly identical. You should be aware of this when you are using JAR files in a build environment. It is recommended that you use versioning information in the manifest file, rather than creation time, to control versions of a JAR file. See the Setting Package Version Information Post.


An Example

Let us look at an example. A simple TicTacToe applet. You can see the source code of this Applet at TicTacToe.java. This demo contains a bytecode class file, audio files, and images having this structure:
TicTacToe folder Hierarchy
TicTacToe folder Hierarchy
The audio and images subdirectories contain sound files and GIF images used by the applet.
You can obtain all these files from jar/examples directory when you download the entire Tutorial online. To package this demo into a single JAR file named TicTacToe.jar, you would run this command from inside the TicTacToe directory:
jar cvf TicTacToe.jar TicTacToe.class audio images
The audio and images arguments represent directories, so the Jar tool will recursively place them and their contents in the JAR file. The generated JAR file TicTacToe.jar will be placed in the current directory. Because the command used the v option for verbose output, you would see something similar to this output when you run the command:
adding: TicTacToe.class (in=3825) (out=2222) (deflated 41%)
adding: audio/ (in=0) (out=0) (stored 0%)
adding: audio/beep.au (in=4032) (out=3572) (deflated 11%)
adding: audio/ding.au (in=2566) (out=2055) (deflated 19%)
adding: audio/return.au (in=6558) (out=4401) (deflated 32%)
adding: audio/yahoo1.au (in=7834) (out=6985) (deflated 10%)
adding: audio/yahoo2.au (in=7463) (out=4607) (deflated 38%)
adding: images/ (in=0) (out=0) (stored 0%)
adding: images/cross.gif (in=157) (out=160) (deflated -1%)
adding: images/not.gif (in=158) (out=161) (deflated -1%)
You can see from this output that the JAR file TicTacToe.jar is compressed. The Jar tool compresses files by default. You can turn off the compression feature by using the 0 (zero) option, so that the command would look like:
jar cvf0 TicTacToe.jar TicTacToe.class audio images
You might want to avoid compression, for example, to increase the speed with which a JAR file could be loaded by a browser. Uncompressed JAR files can generally be loaded more quickly than compressed files because the need to decompress the files during loading is eliminated. However, there is a tradeoff in that download time over a network may be longer for larger, uncompressed files.
The Jar tool will accept arguments that use the wildcard * symbol. As long as there weren't any unwanted files in the TicTacToe directory, you could have used this alternative command to construct the JAR file:
jar cvf TicTacToe.jar *
Though the verbose output doesn't indicate it, the Jar tool automatically adds a manifest file to the JAR archive with path name META-INF/MANIFEST.MF.

In the above example, the files in the archive retained their relative path names and directory structure. The Jar tool provides the -C option that you can use to create a JAR file in which the relative paths of the archived files are not preserved. It's modeled after TAR's -C option.
As an example, suppose you wanted to put audio files and gif images used by the TicTacToe demo into a JAR file, and that you wanted all the files to be on the top level, with no directory hierarchy. You could accomplish that by issuing this command from the parent directory of the images and audio directories:
jar cf ImageAudio.jar -C images . -C audio .
The -C images part of this command directs the Jar tool to go to the images directory, and the . following -C images directs the Jar tool to archive all the contents of that directory. The -C audio . part of the command then does the same with the audio directory. The resulting JAR file would have this table of contents:
META-INF/MANIFEST.MF
cross.gif
not.gif
beep.au
ding.au
return.au
yahoo1.au
yahoo2.au
By contrast, suppose that you used a command that did not employ the -C option:
jar cf ImageAudio.jar images audio
The resulting JAR file would have this table of contents:
META-INF/MANIFEST.MF
images/cross.gif
images/not.gif
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au


Thanks!
Kuldeep

Monday, November 19, 2012

What Is SSHFS?

SSHFS stands for (Secure SHell FileSystem) client that enable us to mount remote filesystem
and interact with remote directories and files on a local machine using SSH File Transfer Protocol
(SFTP). SFTP is a secure file transfer protocol that provides file access, file transfer and file
management features over Secure Shell protocol. Because SSH uses encryption while transferring 
files over the network from one computer to another computer and SSHFS comes with built-in
FUSE (Filesystem in Userspace) kernel module that allows any non-privileged users to create their
file system without modifying kernel code.
Before we move further with the installation process, We’d like to tell you that the below installation
also works on all RedHat based distributions like RHEL 6.3/6.2/6.1/6/5.8, CentOS 6.3/6.2/6.1/6/5.8
and Fedora 17,16,15,14,13,12.

Install SSHFS in RHEL, CentOS and Fedora

Step 1: Installing SSHFS

By default sshfs packages exists on all major Linux distributions, just use the below Yum command
to install it with their dependencies.
# yum install sshfs

Step 2: Creating SSHFS Mount Directory

Once the sshfs package installed, you need to create a mount point directory where you will mount
your remote file system. For example, we have created mount directory under /mnt/sshfs.
# mkdir /mnt/sshfs

Step 3: Mounting Remote Filesystem with SSHFS

Once you have created your mount point directory, now run the following command as a root user to
 mount remote file system under /mnt/tecmint. In your case the mount directory would be anything.
 The following command will mount remote directory called /home/tecmint under /mnt/tecmint in
local system. (Don’t forget replace x.x.x.x with your IP Address and mount point).
# sshfs user@x.x.x.x:/home/sshfs/ /mnt/sshfs

Step 4: Verifying Remote Filesystem is Mounted

If you have run the above command successfully without any errors, you will see the list of remote
files and directories mounted under /mnt/sshfs.
# cd /mnt/sshfs
# ls
[root@ sshfs]# ls
12345.jpg                       ffmpeg-php-0.6.0.tbz2 
Linux 
 

Step 5: Checking Mount Point with df -hT Command

If you run df -hT command you will see the remote file system mount point.
# df -hT
[root@tecmint]# df -hT
Filesystem   Type    Size  Used Avail Use% Mounted on
/dev/sda2 ext3     75G   21G   51G  29% /
/dev/sda5   ext3     24G   21G  1.5G  94% /home
/dev/sda3   ext3     29G   25G  2.6G  91% /data
/dev/sda1   ext3    289M   22M  253M   8% /boot
tmpfs    tmpfs    252M    0  252M   0% /dev/shm
sshfs#user@192.168.1.10:/home/sshfs/ fuse 457G 129G 305G 30% /mnt/sshfs

Step 6: Mounting Remote Filesystem Permanently

To mount remote filesystem permanently, you need to edit the file called /etc/fstab. To do, open the file with your favorite editor.
# vi /etc/fstab
Go to the bottom of the file and add the following line to it and save the file and exit. The below entry mount remote server file system with default settings.
sshfs#user@x.x.x.x:/home/sshfs/ /mnt/sshfs fuse defaults 0 0
Next, you need to update the fstab file to reflect the changes.
# mount -a

Step 7: Unmounting Remote Filesystem

To unmount remote filesystem, jun issue the following command it will unmount the remote file system.
# umount /mnt/sshfs

Excuse for typo!

Thanks!!

Sendmail "cannot open '/etc/mail/local-host-names': World writable directory"

Recently I got some issue with Sendmail. When I tried to check mail queue, then it throw me following error.

/etc/mail/sendmail.cf: line 91: fileclass: cannot open '/etc/mail/local-host-names': World writable directory
e.g.
[root@test ~]# mailq
/etc/mail/sendmail.cf: line 91: fileclass: cannot open '/etc/mail/local-host-names': World writable directory
/etc/mail/sendmail.cf: line 588: fileclass: cannot open '/etc/mail/trusted-users': World writable directory

As Error clearly incdicate that mentioned file have write access for other and group as well and need to change permission for this. So just change permissions and restart sendmail service as below :

$su (If you not login with root)
#chmod go-w /
#chmod go-w /etc
#chmod go-w /etc/mail
#service sendmail restart
[root@test ~]# mailq
                /var/spool/mqueue (5 requests)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
qAHFnjL8006870      166 Sat Nov 17 07:49
                 (Deferred: Connection refused by test.com.)
                                        
qAG9F7d9023285     5631 Fri Nov 16 01:15 MAILER-DAEMON


Sendmail is strict on the permissions. :)

Thanks!!

Sunday, July 22, 2012

lynx Webmaster Tips


Hi Friends,

Today we are going to play with lynx (Command line browser) in Linux. 

Fun in the Terminal With Lynx Browser


Get the text from a Web page as well as a list of links

lynx -dump "http://www.example.com/"

Get the source code from a Web page with Lynx

lynx -source "http://www.example.com/"

Get the response headers with Lynx

lynx -dump -head "http://www.example.com/"

The GNU/Linux command line gives you a lot of small tools that can be connected with each other by piping the output of one tool into another tool.
For example, you might see a page with a lot of links on it that you want to examine more closely. You could open up a terminal and type something like the following:
$ lynx -dump "http://www.example.com" | grep -o "http:.*" >file.txt
That will give you a list of outgoing links on the web page at http://www.example.com, nicely printed to a file called file.txt in your current directory.

Here's how it works:


Lynx is a Web browser that only reads text. This makes it great for extracting text from web pages. The option -dump tells Lynx to grab the web page and display it in the terminal. That is followed by the URL you want to visit. So lynx -dump "http://www.example.com" is just saying, "Lynx, dump the output of http://www.example.com to the screen".

You can try the first part by itself to see what it does, replacing http://www.example.com with another URL of your choice. In the following example I've used the home page of the google.
$lynx -dump  "http://www.google.com/"
You will see output something as below:

   6. https://mail.google.com/mail/?tab=wm
   7. http://www.google.com/intl/en/options/
   8. http://www.google.com/url?sa=p&pref=ig&pval=3&q=http://www.google.com/ig%3Fhl%3Den%26source%3Diglk&usg=AFQjCNFA18XPfgb7dKnXfKz7x7g1GDH1tg
   9. http://www.google.com/history/optout?hl=en
  10. http://www.google.com/preferences?hl=en
  11. https://accounts.google.com/ServiceLogin?hl=en&continue=http://www.google.com/
  12. http://www.google.com/advanced_search?hl=en
  13. http://www.google.com/language_tools?hl=en
  14. http://www.google.com/intl/en/ads/
  15. http://www.google.com/services/
  16. https://plus.google.com/116899029375914044550
  17. http://www.google.com/intl/en/about.html

Extracting the Links from Lynx

Now we can look at the next part of the URL extraction process:
$ lynx -dump "http://www.example.com" | grep -o "http:.*" >file.txt
When you use a pipe (the | symbol), it tells the computer to take the output from the first tool and send it to the following tool. So we are taking the output of Lynx and sending it to gr
e
p

G
rep is a tool to search for text and display each line that contains a matching pattern. The option, -o tells grep to only return the matching part of the line and not the entire line. We are searching for anything that matches "http:.*", which is a simple regular expression.
A regular expression is a pattern that is made up of symbols that tell the computer what to look for in order to make a match. We want to find anything that matches the pattern: http: [and anything that comes after that]. A period (.) in a regular expression symbolizes one character of any type. The asterisk (*) symbolizes zero or more of the preceeding character. So "http.*" means "match 'http' and any number of characters that follow it". This will extract only the URLs from Lynx's output.
We could stop there and just run it as this, which will send the output to the screen:
$ lynx -dump "http://www.example.com" | grep -o "http:.*"
But it would be nice to save the output for later. To save the output to a file, just add the > symbol. In this case the output is being directed to a file named file.txt as shown below.
$ lynx -dump "http://www.example.com" | grep -o "http:.*" >file.txt

Other Options

Here is an example of some other options that you can add. The command sort sorts the results, anuniq removes any duplicate entries.
$ lynx -dump "http://www.example.com" | grep -o "http:.*" | sort | uniq >file.txt


Thanks

Saturday, July 21, 2012

configure a system to use two different networks


Question: 

How we can configure a system to use two different networks.

Requirements:




  • Red Hat Enterprise Linux or *nix OS
  • A system with two Network Interface Cards (NICs)
  • Two different networks


  • Solution:




    • Edit the file  /etc/sysconfig/network and fill in the values of the variables:
    NETWORKING=yes
    HOSTNAME=myhost.example.com
    Note: If necessary, remove or comment out the variable GATEWAY. Each NIC will have it's own gateway.
    • Edit the file /etc/sysconfig/network-scripts/ifcfg-eth0 and add the following variables with their corresponding values:
    DEVICE=eth0
    BOOTPROTO=none
    ONBOOT=yes
    NM_CONTROLLED=no
    USERCTL=no
    IPADDR=
    NETMASK=
    GATEWAY=
    PEERDNS=yes
    DNS1=
    DNS2=
    
    • Do the same for the file /etc/sysconfig/network-scripts/ifcfg-eth1:
    DEVICE=eth1
    BOOTPROTO=none
    ONBOOT=yes
    NM_CONTROLLED=no
    USERCTL=no
    IPADDR=
    NETMASK=
    GATEWAY=
    PEERDNS=yes
    DNS1=
    DNS2=
    
    The values between '<' and '>' are dependent on your network.
    The variables PEERDNSDNS1 and DNS2 are optional. If you have the same DNS servers for both networks, you should put the nameservers IPs in the file /etc/resolv.conf, and in both ifcfg-eth?files remove the variables DNS1 and DNS2 and setPEERDNS=no.
    • Restart the network service:
    # service network restart

    Thanks!