r00t.gr

Programming and more

Entries for the ‘Java’ Category

How to recursive browse a directory with java

import java.io.*; import java.util.*;   class aloxa { public static void main(String args[]) { try { traverse(new File("thomson")); } catch (Exception e) { e.printStackTrace(); } } static public void traverse( File file ) { // Print the name of the entry System.out.println( file ) ; // Check if it is a directory if( file.isDirectory() ) […]

Simple way to get md5 from string in java

You will see a lot of crazy shit when searching for this. I you want something simple, get this function to get md5 from string in java: static String md5(String text) { try { MessageDigest m=MessageDigest.getInstance("MD5"); m.update(text.getBytes(),0,text.length()); return new BigInteger(1,m.digest()).toString(16); } catch (Exception e) { e.printStackTrace(); } return ""; } static String md5(String text) { […]

Create Random String in java.

Useful in many many cases. Here is the code: import java.security.SecureRandom; import java.math.BigInteger;   final class randomstring {   static private SecureRandom rnd= new SecureRandom();   static public String nextRandomString(int size) { return new BigInteger(130, rnd).toString(size); } public static void main(String args[]) { System.out.println(nextRandomString(15));   System.out.println(nextRandomString(32)); }   }import java.security.SecureRandom; import java.math.BigInteger; final class randomstring […]

How to Stop URLConnection from following redirection (Prevent Location: header).

This was very hard for me to find in Google. I almost spend 5 minutes instead of the usual 32 seconds to find the answer. So here it is:   // Disable automatic redirects for all HTTP requests HttpURLConnection.setFollowRedirects(false);   // Disable automatic redirects for a particular connection try { // Create a URLConnection object […]

How to build and sent a POST request with java

This is very useful at many times, if you want to emulate a post request from a browser. I use it all the time, it’s very useful, so i put it here for you to see:   try { // Construct data String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8"); data += "&" […]

Change user on linux (or switch users on linux)

The easiest way to switch users in linux is: su usernamesu username if you type only su then root is default. You can to enter the other user’s password to switch to it.

How to set global maximum heap size (-xmx) for java in linux

Sometimes is usefull to limit the maximum heap size for java. (Especially in a OpenVZ virtual machine(VPS) where java memory managment is terrible: http://www.r00t.gr/2011/03/openvz-vps-and-java-error-and-how-to-fix-it/)You can do this with the xmx parametr of command line java, but if you run many programs, with crons and through other programs, you will need a way to globaly set […]

How to convert byte to String in Java

Java always has something easy implemeted 🙂 So this time, Java String has 2 constructors that take a byte[] array. So: byte[] buf = new byte[1024]; //or whatever […] Fill all the byte array or part of it […] String str = new String(buf);   OR   String str = new String(buf,start,limit); //To create a […]

How to unzip a zip file with java

How to unzip a zip file with java I was writing a script in java, to download the some files from a webserver, then open them and read then, and insert some files into the database. What i noticed was that. when i downloaded the files with my browser, everything was ok, but when i […]

How to remove duplicates in a Vector in java

Remove Dublicate entries in Java Vector I was writing a small java program, that copied some ids from a website. Then the program had to process each one of those ids. I used Vector to store these, then i realized that there are a lot of duplicates. To late to change to something else, Like […]