Thursday, September 29, 2011

JCIFS

The Java CIFS Client Library

JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java. CIFS is the standard file sharing protocol on the Microsoft Windows platform (e.g. Map Network Drive ...). This client is used extensively in production on large Intranets.

1)Download JCIFS using below URL
http://jcifs.samba.org/src/jcifs-1.3.16.zip

2)One sample program for accessing a file of other systems which is in network and writing something on that.

 Logon.java
------------------------------------------------------

import java.io.FileOutputStream;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

public class Logon {
    public static void main( String argv[] ) throws Exception {
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("ISSHYD.COM", "isshyd\\kirankumar.g", "indiangk1791$");
             String path = "smb://192.168.1.209/D$/file1.txt";
        SmbFile sFile = new SmbFile(path, auth);
             SmbFileOutputStream sfos = new SmbFileOutputStream(sFile,true);
            sfos.write("\n append-kiran6".getBytes());
        System.out.println("Done");
      
        // sFile.delete();
    }
}

Note :
Add jcifs.jar in build path before running this program
In this program,
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", "username", "password");

ISSHYD.COM----------->is domaine name of system to access(MyComputer-->Right click-->then see once your domain name of system)(giving domain name is not necessary you can give empty "" )
isshyd\\kirankumar.g-------->is username of system to access
indiangk1791$------------->is password of system to access.
192.168.1.209-------------->IP Address of system to access.

in this program i am accessing file1.txt file in D drive of System and wrote some thing on that file.

Note2:once  see jcifs home/examples folder sample programs.

3)Another Sample program to access all the folders and files of a specific directory.

Here i am accessing all the files and folders of  D drive.

Sample1.java
---------------------------------------------------------------

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;

public class Sample1 {
    public static void main( String argv[] ) throws Exception {
        //String user = "ISSHYD\\kirankumar.g:indiangk1791$";
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domaine", "username", "password");
             String path = "smb://192.168.1.214/D$/";
        SmbFile sFile = new SmbFile(path, auth);
              SmbFile[] files = sFile.listFiles();
        System.out.println("files");
       
        for (SmbFile x : files) {
            System.out.println("file: " + x + ".  path: " + x.getPath() + ".  isDirectory? " + x.isDirectory());
           
       
            if (x.isDirectory()) {
                for (SmbFile dir : x.listFiles()) {
                    System.out.println("dir file: " + dir + ".  path: " + dir.getPath() + ".  isDirectory? " + dir.isDirectory());
                }
            }
        }
    }
}


ANT Tool

1)Download ant tool here

How to find IP Address of System

* you can use command Prompt statement for this.
the commands to find IP Address  are below,
C:\>ipconfig
or
C:\>ipconfig/all

(or)

*Finding IP address using java programme

 FingIPAddress.java
---------------------------------------
import java.net.*;
public class FindIPAddress {
public static void main(String[] args) throws UnknownHostException{
InetAddress ia = InetAddress.getLocalHost();
String s=ia.toString();
System.out.println("IP address to a String is: "+s);
    }
}

Usage of Windows Batch file for java developers

The file which is having (anynameXXX).bat extension then these files are called Windows Batch files.

In this batch file we can write all the commands that we generally use in Command Prompt.
one sample java example with sequence of steps are shown below.

1)create a java class as shown below,

 WindowsBatchFileUsage.java
-------------------------------------------

public class WindowsBatchFileUsage {
    public static void main( String argv[] ) throws Exception {
       
        System.out.println("MyBatch.bat Batch File executed successfully");
    }
}

2)Now i don't want to open my command prompt for compile and execute this java class.
i write a batch file(MyBacth.bat),that contains all the commands to compile and execute my java class.

create a file with extension of .bat ,Right click  edit that file and write following commands and save.
MyBatch.bat
---------------------------------
javac *.java
java WindowsBatchFileUsage
pause


3)Now double click that batch file(MyBatch.bat)
it opens command prompt and executes all the commands that we write in batch file.


Wednesday, September 28, 2011

Create Executable jar

The steps to create Executable jar are as follows,

1) create a java class under the package test.

Executablejar.java
---------------------

package test;
class Executablejar
{
public static void main(String a[])
{
System.out.println("HELLO WORLD");
}

}

2)compile above java class

C:\>javac Executablejar.java -d .

here don't forget to give space between  -d and dot(.) 

3)once run the Executablejar class
to run use following command,
c:\>java test.Executablejar

then you can see "HELLO WORLD" is displayed on console.

4)Then create this manifest file (manifest.txt) with any text editor.

manifest.txt
------------------
Main-Class: test.Executablejar

Note:Don't forget to press enter after write above line and then save.otherwise you get an error
message as "Failed to load Main-Class manifest attribute from
myjar.jar"

5)Next, preparation of executable jar
 
c:\>jar cvfm myjar.jar manifest.txt test
 
here test is the package name of my class .
 
6)Then you are able to start the Executablejar.class by double-clicking on the myjar.jar file (if the JRE is correctly installed) or by typing

c:\>java -jar myjar.jar
 
then you get output as "HELLO WORLD"
 
 
 
Note:
To view the content of the jar file you can use the fallowing command
C:\>jar tf myjar.jar
It displays as:
D:\javaprogramess >jar tf myjar.jar
META-INF/
META-INF/MANIFEST.MF
test/
test/Executablejar.class
 



Read a file using BufferedInputStream

BufferedInputStreamExample.java
----------------------------------------------------------------------------------
import java.io.*;

public class BufferedInputStreamExample {
        public static void main(String[] args) throws Exception {
           
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/logins.txt"));
                while (bis.available() > 0)
                {
                System.out.print((char)bis.read());
                }
                 bis.close();
        }
}

Tuesday, September 27, 2011

Create Folder Using JAVA

FolderCreation.java
-----------------------------------------------------
import java.io.File;
/**
 * @author kirankumarg
 *
 */
public class FolderCreation {
     public static String Path = "D:/SampleFolder/";
    //public static String Path = "D:/SampleFolder/subfolder1/subfolder2/subf3/subf4";
    /**
     * @param args
     */
    public static void main(String[] args) {
        File folder = new File(Path);
        //check whether folder exists or not
        if(!folder.exists()){
            folder.mkdir();//use this for a single folder creation
            //folder.mkdirs();//use this for subfolders creation in a Folder
            System.out.println("Folder has been created Successfully ... ");
        }
    }
}