Wednesday, November 21, 2012

JSP JSTL String length

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"  %>
<c:if test="${fn:length(string)>0}">
...
</c:if>

Saturday, November 3, 2012

Create Temporary File In Java

Get The Current Working Directory In Java

package com.javaee.insight;

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

        String workingDir = System.getProperty("user.dir");
        System.out.println("Current working directory : " + workingDir);

    }
}

Create a File in Java

Setting up ActiveMQ with Tomcat 7

Step 1. Place the activemq-all-5.7.0.jar in [TOMCAT_HOME]/lib directory

Step 2. Open the [TOMCAT_ROOT]/conf/context.xml file and add the below resource to the Context.

<Context>
 <Resource name="jms/ConnectionFactory" 
     auth="Container" 
     type="org.apache.activemq.ActiveMQConnectionFactory" 
     description="JMS Connection Factory"
     factory="org.apache.activemq.jndi.JNDIReferenceFactory" 
     brokerURL="tcp://localhost:61616" 
     brokerName="LocalActiveMQBroker"/>

 <Resource 
    name="jms/queue/Queue" 
    auth="Container" 
    type="org.apache.activemq.command.ActiveMQQueue" 
    description="my Queue"
    factory="org.apache.activemq.jndi.JNDIReferenceFactory" 
    physicalName="APP.QUEUE"/>

</Context>
Step 3. Add the JNDI in your java code.
   InitialContext initCtx = new InitialContext();
        Context envContext = (Context) initCtx.lookup("java:comp/env");
        
        ConnectionFactory connectionFactory = (ConnectionFactory) envContext.lookup("jms/ConnectionFactory");
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("jms/queue/Queue");
        MessageProducer producer = session.createProducer(destination);
        TextMessage msg = session.createTextMessage();
        msg.setText("Message sent");
        producer.send(msg);

Print the Current Project Classpath

Monday, June 18, 2012

Java IO : Convert InputStream to String

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {

        String message = "Hello World";

        InputStream is = new ByteArrayInputStream(message.getBytes());
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        System.out.println(sb.toString());

        br.close();
    }
}

Java IO : Convert String to InputStream

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {
        String str = "Hello World";

        InputStream is = new ByteArrayInputStream(str.getBytes());

        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

        br.close();
    }
}

Saturday, June 16, 2012

Detect OS using Java : System.getProperty()

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

        System.out.println("** Operating System Info **");
        System.out.println();
        System.out.println("Name : " + System.getProperty("os.name"));
        System.out.println("Arch. : " + System.getProperty("os.arch"));
        System.out.println("Version : " + System.getProperty("os.version"));

    }
}

Java IO : Create a new file

Creating a new file in Java using the File.createNewFile() method.

Reference : http://docs.oracle.com/javase/6/docs/api/java/io/File.html#createNewFile()

import java.io.File;
import java.io.IOException;

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

            File file = new File("c:\\sample.txt");

            //true if the named file does not exist and was successfully created;
            // false if the named file already exists
            boolean res = file.createNewFile();

            if (res) {
                System.out.println("File is successfully created!");
            } else {
                System.out.println("File already exists.");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}