0

Rounding double to two decimal places in Java

in
This is the simplest way i found.

Double num = 2.554545;

DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(num));

|
1

Solving "Tomcat - java.lang.OutOfMemoryError: PermGen space" in linux

src : http://www.eukhost.com/forums/f33/tomcat-java-lang-outofmemoryerror-permgen-space-7675/

When you come across following error


java.lang.OutOfMemoryError: PermGen space



Follow the following steps :

1) vi /usr/local/jakarta/tomcat/bin/catalina.sh

2) Add following line into the catalina.sh file.

Quote:
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC"

Partial example of catalina.sh file

Quote:
===========================================
# JSSE_HOME (Optional) May point at your Java Secure Sockets Extension
# (JSSE) installation, whose JAR files will be added to the
# system class path used to start Tomcat.
#
# CATALINA_PID (Optional) Path of the file which should contains the pid
# of catalina startup java process, when start (fork) is used
#
# $Id: catalina.sh 609438 2008-01-06 22:14:28Z markt $
# -----------------------------------------------------------------------------

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC"


# OS specific support. $var _must_ be set to either true or false.
cygwin=false
os400=false
darwin=false

========================================
Save and exit
Then restart the tomcat

Quote:
root@server[~]# /usr/local/jakarta/tomcat/bin/./shutdown.sh
root@server[~]# /usr/local/jakarta/tomcat/bin/./startup.sh
root@server[~]# /usr/local/jakarta/tomcat/bin/./catalina.sh run
root@server[~]# /scripts/restartsrv tomcat

|
0

Problem !!! java.lang.ClassNotFoundException: [Ljava.lang.String

This, java.lang.ClassNotFoundException: [Ljava.lang.String is killing me a lot. I've suspected so many things about it, but actually is quite different. Its also mentioned in sun's bug database

Here are some of the solutions i've found in different sites;

1.http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6434149

This link recommends the following actions to solve this bug:

1) Add -Dsun.lang.ClassLoader.allowArraySyntax=true if you want to use a
library for which you don't have the source with JDK6

2) Change loader.loadClass( name ) to Class.forName( name, false, loader ) if
you own the code.

class org.apache.catalina.loader.WebAppClassLoader
uses this code to load classes:

clazz = loader.loadClass(name);

Can you please change the sections of code where this line is used (1352 and
others) to
clazz = Class.forName( name, false, loader);



2. http://dev.bostone.us/2009/02/23/javaxfacesfacesexception-javalangclassnotfoundexception-ljavalangstring/
  • jdk1.6 unlike 1.5 it will not attempt to load class (java.lang.String) by name by default
  • it is easily solvable by locating JVM runtime in Eclipse's preferences (Window->Preferences->Installed JREs), highlighting the entry and adding runtime parameter -Dsun.lang.ClassLoader.allowArraySyntax=true
links:
  • https://blog.coremedia.com/cm/post/2672591/RE_Java_6_javalangClassNotFoundException_LjavalangString_exception.html
  • http://forums.java.net/jive/thread.jspa?threadID=15823

|
0

A simple secure MIME message SendMail function

Here is a simple secure MIME message sendMail function;


public boolean sendMail(String to, String subject, String text) {

Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", Constants.SMTP_HOST_NAME);
props.put("mail.smtp.port", Constants.SMTP_HOST_PORT);
props.put("mail.smtp.auth", "true");

Authenticator auth = new SMTPAuthenticator();

Session mailSession = Session.getInstance(props, auth);

try{
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(Constants.SMTP_MESSAGE_SENDER));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
message.setHeader("MIM9
message.setHeader("Content-Type", "text/html");
message.setContent(text, "text/html");

transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
return true;
} catch (MessagingException messagingException) {
messagingException.printStackTrace();
return false;
} catch (Exception exception){
exception.printStackTrace();
return false;
}


}

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = Constants.SMTP_AUTH_USER;
String password = Constants.SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}

|
0

Oracle Sequence ID increase by double

if you are using trigger to generate sequence then might happened that the trigger is called twice. So to prevent from increasing the id value here is a little trick

CREATE OR REPLACE TRIGGER TRG_ABC BEFORE INSERT ON ABC
FOR EACH ROW
BEGIN
if :NEW.ID is null
then SELECT ABC_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL;
end if;
END;

|
0

Oracle: use timestamp to convert string to date


You can use the timestamp keyword to convert a string to Date:

select * from retail_transactions where posted_date >= timestamp '2008-12-01 00:00:00';

|
0

5 Regular Expressions Every Web Programmer Should Know

in
Here are 5 Regular Expressions Every Web Programmer Should Know


|

Copyright © 2009 So That I Can Remember All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.