Tuesday, March 29, 2005

HowTo Java-5: Set...Set....Set...Classpath

**To find whether u have java in ur system or not:
%which java
/usr/bin/java

**To see which version of java is in ur system:
%java -version
java version "1.2.2"
Solaris VM (build Solaris_JDK_1.2.2_10, native threads, sunwjit)


**To check your CLASSPATH:
Use the command "echo $CLASSPATH" on the command line.

**Hey In my case no classpath is not showing while typing the command....But program is running.
K..No problem.U can export classpath each time

export CLASSPATH=$CLASSPATH:/path/to/program

**I want to do it permanently

Like in case of Windows,In unix u can do it by editing .profile or .bashprofile or .cshrc

just add this line

export CLASSPATH=/usr/local/jre1.3.1/bin [check the path for urs sys]

Labels:

HowTo Java-4:Hey I am not getting the lib of java in UNIX...???

As we all know UNIX is an UNIQUE`s UNIQUE creation...Famous for its weirdness.I faced manyyy problems at the begining while trying to run java codes.The simple codes run.But when I need to set classpath etc.I used to got stuck.

A simple basic hands-on unix java from the sun site.
1.The /usr/java symbolic link is used to define the default Java environment on a Solaris system when more than one Java environment is installed. Currently, JDK 1.1 is installed in /usr/java1.1, J2SDK 1.2.2 is installed in /usr/java1.2, and J2SDK 1.3.0 and J2SDK 1.3.1 are installed by default in /usr/j2se.

2.Prior to the Solaris 8 release, the /usr/java symbolic link pointed to /usr/java1.1 if both JDK 1.1 and J2SDK 1.2.2 were installed, making JDK 1.1 the default Java platform. Starting with the Solaris 8 release, the /usr/java symbolic link points to /usr/java1.2 by default if both JDK 1.1 and J2SDK 1.2.2 are installed, making J2SDK 1.2.2 the default Java platform.

3.Because there are symbolic links in /usr/bin (also known as /bin) that use /usr/java (for example, /usr/bin/java refers to /usr/java/bin/java), this /usr/java link can change the default Java installation seen by most users. Many Java applications run on any of J2SDK 1.3.1, J2SDK 1.2.2, or JDK 1.1, but users and applications might want to be selective about which Java installation they use.

4.If you want to use JDK 1.1, /usr/java1.1/bin should be on your PATH before /usr/bin. If you want to use J2SDK 1.3.1, /usr/j2se/bin should be on your PATH before /usr/bin.

5.It is possible for root users to make J2SDK 1.3.1 the default Java platform by modifying the /usr/java symbolic link to point to /usr/j2se. However, changing the symbolic link in this manner may cause problems for some Java applications that are expecting to use earlier versions of the Java platform. See the online compatibility documentation for information about incompatibilities between J2SDK 1.2 and J2SDK 1.3.

I pecked all those from....:
http://java.sun.com/j2se/1.3/install-solaris.html

Labels:

Monday, March 28, 2005

HowTo Java 3: Packing Packages........

This time I`ll try to pen down how to create package and how to use it.

NAIL DOWN 1:==============================================
The first thing wat I want to hammer down in ur head is that while creating package ALWAYS REMEMBER THAT THE PACKAGE NAME WILL BE THE SAME AS THE DIRECTORY NAME WHICH CONTAINS IT.
eg.
Say urs directory structure is like
c:\javasource\test.java

N u now wanna to create a package .Urs package name should must be "javasource"

NAIL DOWN 2:
===================================================
While creating package in a java coding the Java source for each class must appear in a file that contains a package statement as its first non-comment statement,

Dint get it...???
K...
See the snippet...

package HasimsPackage;

public class HasimsClass1
{
public static void output()
{
System.out.println("i am c:\\HasimsPackage\\HasimsClass1");
}
}


Now for sake of understanding say we would like to create a packege named as HasimsPackage which will contain two classes.Oh sorry u dont know yet wats a package....!!!
K..

[DEFINITION OF PACKAGE:==========================
A package consists of one or more .class files that occupy a single directory]

Yah,now come back to the point.The two classes u say,HasimsClass1 and HasimsClass2.

NAIL DOWN 3:===============
If you want both classes to be accessible to others, then each class must be declared as public in its own file of the same name(dont laugh...!! obviously it should be),in a directory named HasimsPackage. Yah,this can be a subdirectory.
So I just want u to recollect the things till now.First,if u wanna a package,check where u coding,i mean the path of ur java file which u r coding.Second,just add as the current directory name.Third,make urs class as public.

Well,now create the second class as

package HasimsPackage;

public class HasimsClass2
{
public static void output()
{
System.out.println("i am c:\\HasimsPackage\\HasimsClass2");
}
}

NAIL DOWN 4:=====================
If we have more than one public class in the same file then...??
Yah good question.Only one class can be callable.Not others.

Ok...Now create a Test directory anywhere and carry on coding as follows...

//Test\Test.java
import HasimsPackage.*;

class Test
{
public static void main(String[] args)
{

HasimsClass1.output();
HasimsClass2.output();
}
}
Now compile it from command line
javac -verbose -classpath c:\;. Test.java [supposing the directory is in c:\]

[Verbose is doing noting but giving you a live commentary wats happening in the field...-classpath telling javac to check for packages in c:\ and in current directory.well the output below is self explanatory.its checks test.java.it gets external class( HasimsClass1 and 2)
It loads them.It creates Test.class.it creates HasimsClass1.class and 2.class.....and et all....


C:\Test>javac -verbose -classpath c:\;. Test.java
[parsing started Test.java]
[parsing completed 47ms]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/lang/Object.class)]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/lang/String.class)]
[checking Test]
[loading c:\HasimsPackage\HasimsClass1.java]
[parsing started c:\HasimsPackage\HasimsClass1.java]
[parsing completed 0ms]
[loading c:\HasimsPackage\HasimsClass2.java]
[parsing started c:\HasimsPackage\HasimsClass2.java]
[parsing completed 0ms]
[wrote Test.class]
[checking HasimsPackage.HasimsClass1]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/lang/System.class)]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/io/PrintStream.class)]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/io/FilterOutputStream.class)]
[loading c:\j2sdk1.4.0\jre\lib\rt.jar(java/io/OutputStream.class)]
[wrote c:\HasimsPackage\HasimsClass1.class]
[checking HasimsPackage.HasimsClass2]
[wrote c:\HasimsPackage\HasimsClass2.class]
[total 313ms]

]

Then run it....

C:\Test>java -classpath c:\;. Testi am c:\HasimsPackage\HasimsClass1
i am c:\HasimsPackage\HasimsClass2
=================================
Wats the need to include the "." current directory while running...?
Bcz its need to load the main() method of the starting class(here Test)


Kkkkkkkkkkk.........Its enoughhhhhh.......If I start talking bout classpath......Oh nooo......I`ll pen down....sorry.... key down it later...
Its time to lunch.
:)
Hasim

Labels:

Thursday, March 24, 2005

HowTo JAVA-2:Importing class and packages

This time I`ll to to keep some points in my mind bout importing.

Why should we import?
Obviously its because of code reusing.Java is,infact 70% depends on API and its nothing but code reusing.We use java.util.* Wat does it mean ? It means just find out any specific class inside the package java.util. And that class may be Vector class or any other class.
See the following code

import java.util.*;

class UseVector
{
public static void main(String[] args)
{

Vector v = new Vector();
for (int i = 0; i < 10; ++i)
v.addElement(new Integer(i));
for (int i = 0; i < v.size(); ++i)
System.out.print(v.elementAt(i) + " ");
}
}

We created an instance v of type vector .But where from we got that vector class.Surely it comes from the package
java.util.*

Vector is nothing but a container.Its containing.Well I`ll discuss bout Vector some other day.

Wats that ".*" do?

Nothing,If we import a package with the ".*" ,then it opens a package for name lookup, so when the javac translator can’t find a name in the current directory, it looks in the package java.util to find it.

But wat if we have a Vector class in the current directory?Then the javac will not bother bout that "util" brother at all.It will just pick the current one.So cleaver na?

K.But u`ll say I am alsoa sticky gum.I will make that smart javac to pick that one from the 'util" brother evenif there is one Vector class inside the current directory. Hmmmmm....Then you have two options

1>You can change the import statement to explicitly associate the name Vector with java.util.Vector, as follows:

import java.util.Vector;

2>or, you can use a fully qualified class name and ignore the import altogether:

// Note: no import statement
class UseVector
{
public static void main(String[] args)
{

java.util.Vector v = new java.util.Vector();
.......
............
...........
........
}

Well....I am getting boredddddd.......I`ll post one more on holi and then I`ll go back room.

Labels:

HowTo Java1--Reading a file directly from net...

import java.net.*;
import java.io.*;
import java.util.*;

public class nettest_version4
{
public static void main(String[] args)throws Exception
{
String myurl_strng;
String mysavedfile_strng;
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object

try
{
if (args.length == 0)
{

myurl_strng = getUrlFromUser("Enter the url you would like to parse: ");
System.out.println("###################################################");
System.out.println("u r going to parse "+myurl_strng);
System.out.println("###################################################");
System.out.println("######### please wait checking connection #######");
if (myurl_strng.length() == 0) { return; }
}
else
{
myurl_strng = args[0];
}

//URL ckmds_answer=new URL("http://www.yahoo.com");
URL ckmds_answer=new URL(myurl_strng);
URLConnection ckmds_answer_connection=ckmds_answer.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(ckmds_answer_connection.getInputStream()));
String inputLine;
if (args.length == 0)
{
System.out.println("###################################################");
mysavedfile_strng=getFileNameFromUser("Enter the filename you would like to save: ");
System.out.println("u r going to save "+mysavedfile_strng);
if (mysavedfile_strng.length() == 0) { return; }
}
else
{
mysavedfile_strng = args[0];
}

out = new FileOutputStream(mysavedfile_strng);
// Connect print stream to the output stream
p = new PrintStream( out );
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
p.println(inputLine);
}
p.close();
in.close();
}
catch(Exception ex)
{
System.out.println(ex);
System.out.println("######### A proxy server may be in use #######");
System.out.println("###### Please check it and while compiling #####");
System.out.println("# use java -Dhttp.proxyHost= System.out.println("usage:http://www.yahoo.com");
}
}
// private function that gets console input URL from the user
private static String getUrlFromUser(String prompt) throws IOException
{

System.out.print(prompt);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
// private function that gets console input for saving file from the user
private static String getFileNameFromUser(String prompt) throws IOException
{

System.out.print(prompt);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}

}