Posted by Albin Joseph | Posted in Java | Posted on 17-01-2008
Tagged Under : Java
There is an easy an elegant way to convert or store an exception stack trace to a string variable. The following method takes an exception object as a parameter and returns the string representation of the stack trace.
public String getStackTrace(Exception ex) {
java.io.StringWriter out = new java.io.StringWriter();
ex.printStackTrace(new java.io.PrintWriter(out));
String stackTrace = out.toString();
return stackTrace;
} |
Posted by Albin Joseph | Posted in Java | Posted on 07-01-2008
Tagged Under : Java
Defensive copying is used for protecting the field of an object from being changed by all non native class. Sometimes if we do not use defensive copying our object may hold incorrect data. For eg:
import java.util.*;
public class Test {
private Date today;
public void setToday(Date dt) {
this.today = dt;
}
public Date getToday() {
return today;
}
public static void main(String args[]) {
Test t = new Test();
Date dt = new Date();
t.setToday(dt);
System.out.println("Actual :: " + t.getToday());
dt.setYear(2000);
System.out.println("Current :: " + t.getToday());
}
} |
The output of the above program will be.
Actual :: Mon Jan 07 12:29:43 IST 2008
Current :: Sun Jan 07 12:29:43 IST 3900
In the above program changing the dt object changes t object data also. To prevent this we use defensive copying. The fix for this will be to create a defensive copy of the object in the setter. For eg:
public void setToday(Date dt) {
this.today = new Date(dt.getDate());
} |
Now if we run the program the output will be
Actual :: Mon Jan 07 12:29:43 IST 2008
Current :: Mon Jan 07 12:29:43 IST 2008
Posted by Albin Joseph | Posted in WebSphere | Posted on 10-12-2007
I was in bench for the last few days. Last day when I had a chat with my manager he said that my new project will be in WPS (WebSphere Process Server). I am totally new to WebSphere process server (even I haven’t seen that interface), so I though of learning some WebSphere Process Server stuff. I am not sure from where I came to a conclusion that WebSphere Process Server is something related to web services, anyway it happened. So I though it will be a great idea to learn Web Services tutorial first, before actually jumping into WebSphere Process Server.
As usual I opened my web browser (For most of the time I use Firefox and for serious browsing I use Internet Explorer as I think Internet Explorer is more secure ), and typed “Java web service tutorial”. Great!. more than millions of results came up and as expected the first result was a link to java.sun.com. I became so happy as I am a big fan of Sun Java tutorials. In fact I learned Java by reading the tutorials from Sun only. So I thought this would be great start again.
After the page got opened up, I just started going through the table of contents. In fact I did not want to read the complete tutorial as I was so lazy and I just wanted a Hello World tutorial to start with. I belive all our learning starts with Hello World and that should be the first program every developer should try
. But amazingly I did not find a Hello World web service tutorial there. I could see everything except web service in Sun’s web service tutorial. They are talking about JAXB, STAX, ABCD, XYZ etc. But no web service. Amazingly I couldn’t find any Hello World tutorial in most of the web service tutorial links I got from google search.
Did I miss something? or there is no Hello World web service tutorial or what? Its very unfortunate that there is no hello world web service tutorial on net. After reading this you may ask why you are not writing a hello world tutorial on web service? The answer is very simple, still I am in search for a Hello World tutorial. Let me write a Hello World web service, after that I will write the tutorial.
Posted by Albin Joseph | Posted in Java | Posted on 29-11-2007
How to connect to internet from Java program through a proxy? The answer is very simple Just add the following arguments to the virtual machine
java -Dhttp.proxyHost=hostname -Dhttp.proxyPort=port -Dhttp.proxyUser=username -Dhttp.proxyPassword=passowrd |
or just put set these System properties using the code.
System.getProperties().put("http.proxyHost", "hostname");
System.getProperties().put("http.proxyPort", "port");
System.getProperties().put("http.proxyUser", "username");
System.getProperties().put("http.proxyPassword", "password"); |
If your proxy doesn’t require an authentication, you just need to set proxyHost and proxyPort properties only.
But the fist method wouldn’t work if we are running our program from within Eclipse as we don’t have command line access. So to set the proxy properties in Eclipse just go to Run menu and click on Run. A dialog box would appear and in the first tab (Main tab) enter your main class and other information and in the Arguments tab under VM arguments just add the following line.
-Dhttp.proxyHost=hostnmae -Dhttp.proxyPort=port |
Posted by Albin Joseph | Posted in Java | Posted on 20-10-2007
The following Java code plays a .wav file
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
public class MusicPlayer {
public static void main(String args[]) {
try {
AudioInputStream stream =
AudioSystem.getAudioInputStream(
new File("C:\\i386\\Blip.wav"));
AudioFormat format = stream.getFormat();
DataLine.Info info =
new DataLine.Info(Clip.class,
stream.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
}
} |