To check the maximum number of files allowed for a user on Linux run the following command:
$ ulimit -n
To view the maximum number of files allowed on the system as a whole:
$ cat /proc/sys/fs/file-max
Wednesday, January 8, 2014
Monday, May 13, 2013
Removing a file/directory that starts with a "-" in Linux/Unix
Removing a file/directory that starts with the character "-" can be tricky in Linux/Unix. On Linux/Unix you may get this error when attempting to remove a file/directory:
Some commands may give you a hint e.g.
This is exactly what is required i.e just append the "./" to the file/folder to resolve your problem.
rm: invalid option -- '1'Some commands may give you a hint e.g.
Try `rm ./-1' to remove the file `-1'.This is exactly what is required i.e just append the "./" to the file/folder to resolve your problem.
Friday, May 10, 2013
Slow startup of Weblogic server on Linux
Had a strange problem on our Linux server recently after installing Weblogic 10. The server was very slow to startup. Seems the problem is this bug in Java 5: Bug 6202721
The fix is to edit the java.security file in the jre/lib/security folder of your java installation on the server. Alter the securerandom.source field to be file:/dev/./urandom as suggested in this article: How to improve Weblogic Servers Startup Time
The fix is to edit the java.security file in the jre/lib/security folder of your java installation on the server. Alter the securerandom.source field to be file:/dev/./urandom as suggested in this article: How to improve Weblogic Servers Startup Time
Friday, January 11, 2013
NullPointerException with Hibernate 4
java.lang.NullPointerException at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:207) at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
The exception above can be resolved by adding this Hibernate property:
<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
Thursday, January 10, 2013
"No runnable methods" when using Maven for a class that shouldn't be Tested
I recently ran into a problem doing a Maven build where I had a utility class to be used for my Test cases. Adding the @Ignore tag didn't seem to help. It turns out the solution was simply to add this plugin entry to my pom.xml file as my file had the suffix Util.java:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*Util.java</exclude>
</excludes>
</configuration>
</plugin>
Tuesday, October 9, 2012
ClassNotFoundException: org.hibernate.hql.ast.HqlToken
When using Hibernate to read from a Database using Weblogic 10 and Springframework 2.5.6, I encountered the following error:
ClassNotFoundException: org.hibernate.hql.ast.HqlToken
The reason for this is that it by default will use Weblogic's default classloader to load the antlr jar file and doesn't use the version included within your .war file.
To solve this you need to wrap your .war in an ear file and within the META-INF/weblogic-application.xml file within you .ear file include the following:
<prefer-application-packages>
<package-name>antlr.*</package>
</prefer-application-packages>
This should resolve this issue.
ClassNotFoundException: org.hibernate.hql.ast.HqlToken
The reason for this is that it by default will use Weblogic's default classloader to load the antlr jar file and doesn't use the version included within your .war file.
To solve this you need to wrap your .war in an ear file and within the META-INF/weblogic-application.xml file within you .ear file include the following:
<prefer-application-packages>
<package-name>antlr.*</package>
</prefer-application-packages>
This should resolve this issue.
Sunday, August 26, 2012
SQL String functions
Some STRING functions in SQL:
select RIGHT(column, chars) from table;
select SUBSTRING_INDEX(column, char, which char (i.e, 1,2...)) from table;
select REVERSE(string);
select LTRIM(string);
select RTRIM(string);
select LENGTH(string);
select RIGHT(column, chars) from table;
select SUBSTRING_INDEX(column, char, which char (i.e, 1,2...)) from table;
select REVERSE(string);
select LTRIM(string);
select RTRIM(string);
select LENGTH(string);
Monday, July 30, 2012
Java Pitfalls Book
This book "Java™ Puzzlers: Traps, Pitfalls, and Corner Cases
It explains what is wrong with this piece of code (It doesn't print out AB as one would expect):
public class PrintingSomeLetters {
public static void main(String[] args) {
System.out.println('A' + 'B');
}
}
Monday, July 23, 2012
Getting a list of installed services on a Linux server
$ chkconfig --list
Friday, June 22, 2012
Tuesday, May 15, 2012
Java 5 autoboxing fail on equality check
Double d1 = 25d;
Double d2 = 25d;
if (d1 == d2) {
System.out.println("equal (==)");
} else if (d1.equals(d2)) {
System.out.println("equal (using equals() method)");
}
The output is:
equal (using equals() method)
Double d2 = 25d;
if (d1 == d2) {
System.out.println("equal (==)");
} else if (d1.equals(d2)) {
System.out.println("equal (using equals() method)");
}
The output is:
equal (using equals() method)
Wednesday, May 2, 2012
Reverse search for a previous command on Linux
Use CTRL-r to reverse search through a history of your previous commands executed in Linux e.g.
Type in the text for a command you are searching for e.g. 'cat'
In this case the last command executed with the substring 'cat' is 'cat test.txt'.
To exit the reverse search just use CTRL-g.
$(reverse-i-search)`':
Type in the text for a command you are searching for e.g. 'cat'
$(reverse-i-search)`cat': cat test.txt
In this case the last command executed with the substring 'cat' is 'cat test.txt'.
To exit the reverse search just use CTRL-g.
Thursday, April 26, 2012
Using SED to Append or Prepend characters
You can use sed to Append/Prepend characters to a String e.g.
We have a file (test.txt) below and we would like to Prepend each capital letter with a space:
sed makes use of a special character "&" which implies the pattern found.
Similarly we could Append a sentence after the "Hello." string:
We have a file (test.txt) below and we would like to Prepend each capital letter with a space:
$ cat test.txt
Hello.ThisIsATestFile.
$ sed "s/[A-Z]/ &/g" test.txt
Hello. This Is A Test File.
sed makes use of a special character "&" which implies the pattern found.
Similarly we could Append a sentence after the "Hello." string:
$ sed "s/Hello./&HowAreYou?/g" test.txt
Hello.HowAreYou?ThisIsATestFile.
Thursday, March 8, 2012
Configuring maven to autogenerate Serializable JAXB classes
First you need to create a bindings file:
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:globalBindings>
<jxb:serializable uid="1"/>
</jxb:globalBindings>
</jxb:bindings>
Save this file under "src/main/resources/" of your maven module. You can name it whatever you want e.g. jaxb-bindings.xjb.
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<id>Request</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>*/*.xsd</include>
</schemaIncludes>
<generatePackage>mygeneratedfiles</generatePackage>
<generateDirectory>src/main/java</generateDirectory>
<bindingDirectory>src/main/resources</bindingDirectory>
<bindingIncludes>
<bindingInclude>jaxb-bindings.xjb</bindingInclude>
</bindingIncludes>
</configuration>
</execution>
</executions>
</plugin>
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:globalBindings>
<jxb:serializable uid="1"/>
</jxb:globalBindings>
</jxb:bindings>
Save this file under "src/main/resources/" of your maven module. You can name it whatever you want e.g. jaxb-bindings.xjb.
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<id>Request</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>*/*.xsd</include>
</schemaIncludes>
<generatePackage>mygeneratedfiles</generatePackage>
<generateDirectory>src/main/java</generateDirectory>
<bindingDirectory>src/main/resources</bindingDirectory>
<bindingIncludes>
<bindingInclude>jaxb-bindings.xjb</bindingInclude>
</bindingIncludes>
</configuration>
</execution>
</executions>
</plugin>
Tuesday, March 6, 2012
Getting the first day of the month in Oracle
select TRUNC(sysdate, 'MM') from dual;
Getting the last day of the month in Oracle
select trunc(last_day(sysdate)) from dual;
Thursday, December 8, 2011
Using a Singleton
Below is an example of a Singleton used to decrement a counter variable:
package singleton;
public class Counter {
private static Counter instance;
private int count;
private Counter() {
}
public static synchronized Counter getInstance() {
if (instance == null) {
instance = new Counter();
}
return instance;
}
public synchronized void decrement() {
count--;
if( count<0 ) {
count=0;
}
}
public synchronized int getCount() {
return count;
}
public synchronized void setCount(int countValue) {
count = countValue;
}
}
Test class for the singleton above:
package singleton;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class CounterTest {
private Counter counter1 = null, counter2 = null;
@Before
public void setUp() throws Exception {
counter1 = Counter.getInstance();
counter2 = Counter.getInstance();
}
/**
* Test case to ensure that the getInstance Method
* returns a non null object and to ensure
* that getting 2 instances of Counter results in
* the same object being returned.
*/
@Test
public void testGetInstance() {
counter1 = Counter.getInstance();
assertNotNull(counter1);
counter2 = Counter.getInstance();
assertNotNull(counter2);
assert (counter1 == counter2);
}
/**
* Test case that tests decrementing the count variable and
* also ensures that both counter1 and counter2
* returns the same count value.
*/
@Test
public void testDecrement() {
counter1.setCount(20);
counter1.decrement();
assert (counter1.getCount() == 19);
assert (counter2.getCount() == 19);
}
/**
* Tests that both instances of the Count returns
* the same count value.
*/
@Test
public void testGetCount() {
int count1 = counter1.getCount();
int count2 = counter2.getCount();
assert (count1 == count2);
counter2.setCount(100);
count1 = counter1.getCount();
assert (count1 == 100);
count2 = counter2.getCount();
assert (count1 == count2);
}
/**
* Tests that we are allowed to set the count variable
* and that another instance reading that variable will
* result in the correct value being returned. */
@Test
public void testSetCount() {
counter2.setCount(100);
int count1 = counter1.getCount();
assert (count1 == 100);
}
}
package singleton;
public class Counter {
private static Counter instance;
private int count;
private Counter() {
}
public static synchronized Counter getInstance() {
if (instance == null) {
instance = new Counter();
}
return instance;
}
public synchronized void decrement() {
count--;
if( count<0 ) {
count=0;
}
}
public synchronized int getCount() {
return count;
}
public synchronized void setCount(int countValue) {
count = countValue;
}
}
Test class for the singleton above:
package singleton;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class CounterTest {
private Counter counter1 = null, counter2 = null;
@Before
public void setUp() throws Exception {
counter1 = Counter.getInstance();
counter2 = Counter.getInstance();
}
/**
* Test case to ensure that the getInstance Method
* returns a non null object and to ensure
* that getting 2 instances of Counter results in
* the same object being returned.
*/
@Test
public void testGetInstance() {
counter1 = Counter.getInstance();
assertNotNull(counter1);
counter2 = Counter.getInstance();
assertNotNull(counter2);
assert (counter1 == counter2);
}
/**
* Test case that tests decrementing the count variable and
* also ensures that both counter1 and counter2
* returns the same count value.
*/
@Test
public void testDecrement() {
counter1.setCount(20);
counter1.decrement();
assert (counter1.getCount() == 19);
assert (counter2.getCount() == 19);
}
/**
* Tests that both instances of the Count returns
* the same count value.
*/
@Test
public void testGetCount() {
int count1 = counter1.getCount();
int count2 = counter2.getCount();
assert (count1 == count2);
counter2.setCount(100);
count1 = counter1.getCount();
assert (count1 == 100);
count2 = counter2.getCount();
assert (count1 == count2);
}
/**
* Tests that we are allowed to set the count variable
* and that another instance reading that variable will
* result in the correct value being returned. */
@Test
public void testSetCount() {
counter2.setCount(100);
int count1 = counter1.getCount();
assert (count1 == 100);
}
}
Thursday, October 20, 2011
java.rmi.MarshalException: Failed to serialize Error
This error occurs when trying to invoke a Remote object that is not Serializable. You can fix this by making the object in question (if possible) Serializable by implementing java.io.Serializable.
Thursday, September 22, 2011
Converting from java.util.Date to XMLGregorianCalendar
If you need to convert from java.util.date to an XMLGregorianCalendar format you can use the following code:
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import java.util.GregorianCalendar;
public class DateConversion {
public XMLGregorianCalendar dateToXMLGregorianCalendar(
final java.util.Date date)
throws DatatypeConfigurationException {
DatatypeFactory df = DatatypeFactory.newInstance();
 if (date == null) {
return null;
} else {
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(date.getTime());
return df.newXMLGregorianCalendar(gc);
}
}
}
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import java.util.GregorianCalendar;
public class DateConversion {
public XMLGregorianCalendar dateToXMLGregorianCalendar(
final java.util.Date date)
throws DatatypeConfigurationException {
DatatypeFactory df = DatatypeFactory.newInstance();
 if (date == null) {
return null;
} else {
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(date.getTime());
return df.newXMLGregorianCalendar(gc);
}
}
}
Touch all files within the current directory
If you need to update all files within the current directory with the latest timestamp you can use the following command:
find . -type f -exec touch {} \;
find . -type f -exec touch {} \;
Subscribe to:
Posts (Atom)