Monday, November 7, 2011

Truncate nohup.out file in linux

While sending your program output to file called nohup.out, you can't simply delete the file. Since, it is handled in server memory. In that case, you have to empty file using following command.

If you delete nohup.out, while running your program, it will leads to server memory growing and become server unstable.

change the working directory to location where your nohup.out file reside. Then execute the following command to empty the file.

cat /dev/null >nohup.out

NOTE :- nohup.out is in linux for gathering program output data. If you run application as following output will redirect to the nohup.

nohup java -jar testApp.jar

Tuesday, October 25, 2011

Find multicast groups

In windows, use following  commands to find multicast groups.

Open command prompt and run the following command in terminal.

netsh interface ip show joins

Friday, September 16, 2011

Find contents in files

Suppose, there are many files that contains "CONTENT" you want to search, use the following command in the Linux terminal.

grep "CONTENT" *.*

Friday, September 9, 2011

Create Scheduler in Java


Some applications need to run on a scheduled time. This java application runs on every Sunday midnight.

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ScheduleTest extends TimerTask {

public void run() {
         System.out.println("Schedule working ");
         //TODO
      }
}

class Application {

public static void main(String[] args) {

    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
       
         date.set(
                  Calendar.DAY_OF_WEEK,
                  Calendar.SUNDAY
         );

         date.set(Calendar.HOUR, 0);
         date.set(Calendar.MINUTE, 0);
         date.set(Calendar.SECOND, 0);
         date.set(Calendar.MILLISECOND, 0);

// Schedule to run every Sunday in midnight

         timer.schedule(
         new ScheduleTest (),
         date.getTime(),
         1000 * 60 * 60 * 24 * 7
         );
    }
}

Tuesday, August 30, 2011

Conversion between signed & unsigned byte

Convert byte value from signed to unsigned

Use '&' operation with 0xFF  as follows.
Suppose signed byte value is byt (-120,64,....).

    byt & 0xFF

Above will convert signed to unsigned and return unsigned value.

Convert byte value from unsigned to signed

    ByteBuffer byteBuff = ByteBuffer.allocate(4);
    byteBuff.putInt(intValue);
    by[i] = byteBuff.get(3);


Above code convert  unsigned values to sign byte values.

Sunday, August 21, 2011

How to get System information in windows

  • Open command prompt
  • Type "systeminfo" command
  • Run the command
This will get a few seconds to load information.

Thursday, August 11, 2011

View Linux version using command

Use following command to get linux version.

uname -a

Monday, August 8, 2011

Mysql set auto increment start value

In default, auto increment values are started in 0(zero). Suppose you, want to start value auto increment with 1234, use the following command after table creation.

ALTER TABLE TableName AUTO_INCREMENT=1234

Wednesday, July 20, 2011

Get last bash command in shell

Suppose you want to get previously used command without using up / down arrows,

Simply type !(command start char).

Example :-

If you want to get "ps -eaf|grep java", simply type !p in the bash shell.

NOTE :- This will return last executed command.

RSA java encrypt, decrypt

Following code can be used to encrypt, decrypt string using RSA encryption.

NOTE :- Apache Commons Codec used for encode string to base64.

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;

public class MainClass {

    public static void main(String[] args) throws Exception {
        byte[] expBytes = Base64.decodeBase64("AQAB");
        byte[] modBytes = Base64.decodeBase64("nzLiZDSiu484r5NcBQN3rNP3x5aqY3Eq6CkQDwuilTzd5ZNdTcTxw7C1JQ9ih27Vq4RU9NYgi9oOUTVQ2gkqP1OJA9aawjCRwMJ7PRyKlBEpsdE/wFtu9/1ciGRtWSyACr2jTASZPQa+aHQh2qziacWd+iVmGIq0+l11nGG/GYU=");
        byte[] dBytes = Base64.decodeBase64("nyF45NssUzkdW3t7/tLxfENBKTN0TARh9ECfebqSoIR/9awxFrynQYnP+CSBw4jJcjHLzhR/4etsZkZZ9Cg3HhPA5pjVcI5kJct4kLjWM+ejZliZoV/KvpJN261VKKLTJMX64UeMiLAlb7mUNoNqKztgflxz5Dbad5hemvgwg50=");

        BigInteger modules = new BigInteger(1, modBytes);
        BigInteger exponent = new BigInteger(1, expBytes);
        BigInteger d = new BigInteger(1, dBytes);

        KeyFactory factory = KeyFactory.getInstance("RSA");
        Cipher cipher = Cipher.getInstance("RSA");
        String input = "test";

        RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponent);
        PublicKey pubKey = factory.generatePublic(pubSpec);
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8"));
        System.out.println(encrypted);
        System.out.println("encrypted: " + new String(encrypted));

        RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);
        PrivateKey privKey = factory.generatePrivate(privSpec);
        cipher.init(Cipher.DECRYPT_MODE, privKey);
        byte[] decrypted = cipher.doFinal(encrypted);
        System.out.println("decrypted: " + new String(decrypted));
    }
}

Thursday, May 26, 2011

vi editor command


When you use Linux vi editor above commands will help you.

Tuesday, March 29, 2011

Java script to get list box text

When you want to get list box selected text from your application using java script use the following js code.

var w = document.getElementById('picklistid').selectedIndex;
var selected_text = document.getElementById('picklistid').options[w].text;

selected_text variable will be returned the combo box selected text.

Wednesday, March 9, 2011

Mysql Date

Some useful SQL commands to get dates in Mysql server.

select curdate() 3/10/2011
select curdate()-1 20110309
select curdate() - INTERVAL 1 DAY 3/9/2011

Sunday, February 27, 2011

Mysql enable logging

mysql > tee output.log

Mysql has default built in function to logging transactions. In Mysql client, type above command. It will enabled logging in transaction. "output.log" file is created in Mysql bin directory.
 

Sunday, February 6, 2011

Find process id in linux

Use the below command to find the process id of your application. Here app name is "TestApp.jar".

ps -ef | grep TestApp

Run .jar file with log

Use the following command to run jar file for logging errors.

nohup java -jar -Xms1024m -Xmx2048m Test.jar &

Above command will run the "Test.jar" file in background and produce error file called "nohup" in same directory. ( All system out prints will be redirected into this file). So, developers can catch errors using the "nohup".

Wednesday, February 2, 2011

Mysql Current date & time

Use the Mysql server curdate() and curtime() functions to get current date & time values from the system.
Below SQL demos, the usage of the functions.

insert into test(cur_date,cur_time) values(curdate(),curtime());

Thursday, January 6, 2011

Mysql data unload

Use the following SQL to unload data from Mysql server without executing command. This method is good for very large set of data.

select * from table into outfile "file.txt"


Note :- file.txt will be created at"MySQL/data/database" directory.