Public snipts » java The latest public java snipts.

showing 1-20 of 58 snipts for java
  • Initialisierungsreihenfolge
     
    public class Init {
      String f(String s) {
        System.out.println(s);
        return s;
      }
    
      String init = f("Definition");
    
      {
        init = f("Initblock");
      }
    
      Init() {
        init = f("Ctor");
      }
    
      public static void main(String[] args) {
        new Init();
      }
    }
    

    copy | embed

    0 comments - tagged in  posted by OutOfBrain on Feb 19, 2010 at 3:27 a.m. EST
  • log eclipse errors to the console
     
    ./eclipse -consolelog 
    

    copy | embed

    0 comments - tagged in  posted by pigletto on Feb 11, 2010 at 2:21 a.m. EST
  • Getting a logger with Log4j
     
    private static final Log LOG = LogFactory.getLog(SomeClassName.class);
    

    copy | embed

    0 comments - tagged in  posted by lenni on Feb 09, 2010 at 4:43 a.m. EST
  • Init an Attribute with Inheritance
     
    class BaseInit
    {
      int x = this.foo();
    
      int foo()
      {
        return 0;
      }
    }
    
    class SubInit extends BaseInit
    {
      @Override
      int foo()
      {
        return 1;
      }
    }
    
    public class InheritanceAttributeInit
    {
      public static void main(String[] args)
      {
        BaseInit b = new SubInit();
        System.out.println(b.x); // 1
      }
    }
    

    copy | embed

    0 comments - tagged in  posted by OutOfBrain on Feb 02, 2010 at 5:29 a.m. EST
  • Running a class inside a jar
     
     java -cp konsumtools.jar de.myToys.konsum.tools.export.Sitemapper
    

    copy | embed

    0 comments - tagged in  posted by lenni on Jan 22, 2010 at 5:33 a.m. EST
  • A little bit quicker for loop
     
    int c = 1000; // example value (can be whatever)
    
    for (int i = -1, len = c; ++i < len;)
    {
      ; // do something
    }
    

    copy | embed

    0 comments - tagged in  posted by d1s4st3r on Jan 16, 2010 at 4:03 a.m. EST
  • Print a file line for line in Java (the 1.6 way)
     
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class SlugTester
    {
    
        static String FILENAME = "C:\\slug-test.txt";
    
        public static void main(String[] args)
        {
            try
            {
                BufferedReader reader = new BufferedReader(new FileReader(FILENAME));
    
                String line;
                while ((line = reader.readLine()) != null)
                {
                    System.out.println(line);
                }
    
            } catch (FileNotFoundException e)
            {
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by lenni on Jan 12, 2010 at 4:03 a.m. EST
  • Java regex tester
     
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class RegexTester {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    	// TODO Auto-generated method stub
    
    	String regex = "/ajax/\\w+/(.*)";
    
    	String[] strings = { "/ajax/hello/KID/de-mt.vc.ca02.06.04/1559286",
    		"/ajax/hell_o/KID/de-mt.vc.ca02.06.04/1559286",
    		"/ajax/h0e-l8l_o/KID/de-mt.vc.ca02.06.04/1559286", };
    
    	for (String s : strings) {
    	    Pattern pattern = Pattern.compile(regex);
    	    Matcher matcher = pattern.matcher(s);
    
    	    String kid = "";
    	    if (matcher.find()) {
    		// group(0) returns the entire match, not a captured group
    		kid = matcher.group(1);
    	    }
    
    	    System.out.println(kid);
    	}
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by lenni on Dec 28, 2009 at 4:23 a.m. EST
  • variations (without repetitions) generator
     
    	public void generate(Integer leng) {
            Integer[] variation = new Integer[leng]; 
            this.putElement(leng, variation, 0);
    	}
    //Ad. public void generate(Integer)
    //variation to poczatkowa pusta tablica, ktora jest jakby modelem
    //wariacji (model okresla tylko dlugosc)
    	
        private boolean repetition(Integer[] var, Integer element, Integer position)
        {
            for(int j=0;j<position;j++)
                if(var[j]==element)
                    return true;
            return false; //sprawdza czy na dotychczasowych pozycjach nie ma
                          // takiego samego elementu
        }
        
        private void putElement(Integer leng,Integer[] var,Integer position)
        {
            if(position >= leng)
            {
               //tutaj nalezy odwolac sie do kontekstu calej klasy
               //sprawdzenie warunku wymaganego w zadaniu - NVM
                Integer[] clone_var = (Integer[])var.clone();
                this.wartosci.clear();
    
            	if(clone_var.length == this.zmienne.size() && this.wartosci.isEmpty() == true) {
            		for(int i = 0; i < clone_var.length; i++) this.wartosci.add(clone_var[i]);
            		this.solveIt();
            		
            	}
            	return;
            	
            }
            else
            {
                for (int i=0;i<values.length;i++)
                {
                    if(!this.repetition(var,values[i],position))
                    { //jezeli dany element sie nie powtorzyl, to wywolujemy  
                      //kolejne funkcje i chuj - to jest w chuj nieoptymalne
                    	var[position] = values[i];
                        this.putElement(leng, var, position+1);
                    }
                }
            }
        }
    

    copy | embed

    0 comments - tagged in  posted by msx on Dec 27, 2009 at 9:45 a.m. EST
  • add/del a directory and a file
     
    import java.io.*;
    class MyClass1
    	{
    	
    	public static void main(String[] args) throws IOException
    		{
    		String currentDirectory="/java";
    		String s[];
    		File d=new File("new folder");
    		d.mkdir();
    		
    		
    		
            	File f1 = new File("f1.txt");
        		File f2=new File("f2.txt");
            	
           		f1.createNewFile();
    		f1.renameTo(f2);
            	
    		File dir=new File(currentDirectory);
    		s=dir.list();
        		for(int i=0;i<s.length;i++)
    			System.out.println(s[i]+"\n");
    			
            	d.delete();	
    		}
    	
    	}
    

    copy | embed

    0 comments - tagged in  posted by rohini_hino on Dec 21, 2009 at 10:38 a.m. EST
  • read 2 nos from keyboard and find quotient
     
    import java.io.*;
    public class Read
    	{
    	public static void main(String args[]) throws IOException
    		{
    		
    		
    		String s;
    		int i=0,res=0;
    		int tmpValue[]=new int[2];
    		DataInputStream din = new DataInputStream(System.in);
    		while((s = din.readLine()) != null)
    		{
    		try
    			{
    			tmpValue[i++] = Integer.parseInt(s);
    			
    			}
    		catch(NumberFormatException ne)
    			{
    			System.out.println("give integers only");
    			}
    		
    		catch(ArrayIndexOutOfBoundsException ee)
    			{
    			System.out.println("give only 2 nos with a control z");
    			}
    			
    		}
    		try
    			{
    			res=tmpValue[0]/tmpValue[1];
    			System.out.println("Quotient="+res);
    			}
    		catch(ArithmeticException ae)
    			{
    			System.out.println("not possible to divide by zero");
    			}
    			
    		}
    	}
    

    copy | embed

    0 comments - tagged in  posted by rohini_hino on Dec 20, 2009 at 10:22 a.m. EST
  • reads lines from keyboard till user presses control z and writes it to a file
     
    //Reader.java
    import java.io.*;
    public class Reader
    	{
    	public static void main(String args[]) throws IOException
    		{
    		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    		String str;
    		File f=new File("file.txt");
    		FileWriter fw=new FileWriter(f);
    		PrintWriter bw=new PrintWriter(fw);
    		int i=0;
    		int j=0;
    		do
    			{
    			str=br.readLine();
    			bw.println(str);
    			}while (str != null);
     		
    			
    		fw.close();
    		bw.close();
    			
    		}
    	}
    	
    

    copy | embed

    0 comments - tagged in  posted by rohini_hino on Dec 20, 2009 at 9:52 a.m. EST
  • Exception handling
     
    //AgeException.java
    
    public class AgeException extends Exception	
    	{
    	private String message;
    	public AgeException(String message)
    		{
    		super(message);
    		}
    	}
    //Admission.java
    
    
    public class Admission
    	{
    	public int age;
    	public String name;
    	public Admission(int age,String name)
    		{
    		this.age=age;
    		this.name=name;
    		}
    	public void checkAge() throws AgeException
    		{
    		if(age<19 || age>24)
    			{
    			AgeException ae=new AgeException("Age entered should be between 19 and 24\n Student name="+name +" Student age="+age);
    			throw ae;
    			}
    			
    		}
    
    //GetAdmission.java
    
    public class GetAdmission
    	{
    	public static void main(String args[])
    		{
    		Admission A=new Admission(Integer.parseInt(args[0]),args[1]);
    		
    			try
    				{
    			
    				A.checkAge();
    				}
    			catch(AgeException ae)
    				{
    				System.out.println(ae);
    				}
    			
    		
    		}
    	}
    

    copy | embed

    0 comments - tagged in  posted by rohini_hino on Dec 17, 2009 at 4:45 a.m. EST
  • Android ColorMatrixColorFilter usage example.
     
    ImageView imageView = (ImageView)findViewById(R.id.imageView);
    float[] matrix = new float[] {
    	0, 0, 0, 0, 0,
    	0, 1, 0, 0, 0,
    	0, 0, 1, 0, 0,
    	0, 0, 0, 1, 0,
    };
    imageView.setColorFilter(new ColorMatrixColorFilter(matrix));
    imageView.invalidate();
    

    copy | embed

    0 comments - tagged in  posted by hongster on Nov 29, 2009 at 7:09 a.m. EST
  • modify netbeans startup configuration
     
    modify netbeans.conf 
    
    netbeans_default_options="-J-Dorg.netbeans.modules.tomcat.autoregister.token=1259115474906 -J-Dorg.netbeans.modules.tomcat.autoregister.catalinaHome=\"C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.20\" -J-client -J-Xss2m -J-Xms256m -J-XX:PermSize=256m -J-XX:MaxPermSize=256m -J-Dnetbeans.logger.console=true -J-ea -J-Dapple.laf.useScreenMenuBar=true -J-Dsun.java2d.noddraw=true"
    

    copy | embed

    0 comments - tagged in  posted by iamacnhero on Nov 28, 2009 at 5:03 a.m. EST
  • Anonymous thread with init
     
        new Thread() {
          private String someVar;
          private int    someOtherVar;
          
          public void run() {
            // threadcode
          }
          
          public void init(String someVar, int someOtherVar) {
            this.someVar = someVar;
            this.someOtherVar = someOtherVar;
            // other initcode
            start();
          }
        }.init("", 0);
    

    copy | embed

    0 comments - tagged in  posted by OutOfBrain on Nov 25, 2009 at 5:02 a.m. EST
  • adicionar mensagem no faces
     
     public static void addMensagemErro(String mensagem, FacesMessage.Severity severity) {
            FacesMessage msg = new FacesMessage(mensagem);
            msg.setSeverity(severity);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    

    copy | embed

    0 comments - tagged in  posted by mwanalezi on Nov 14, 2009 at 2:34 p.m. EST
  • ajust memory use in jira
     
    ajust memory use in jira
    
    modfiy %JIRA_HOME%/bin/setenv.sh, modify varaible JAVA_OPTS:
    set JAVA_OPTS=%JAVA_OPTS% -Xms512m -Xmx768m -Datlassian.standalone=JIRA -Dorg.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER=true
    
    
    // -Xms256m: indicate mininum memory, -Xmx512m: indicate the maximum memory
    
    and set:
    
    JIRA_MAX_PERM_SIZE=196m
    

    copy | embed

    0 comments - tagged in  posted by iamacnhero on Nov 03, 2009 at 10:10 p.m. EST
  • Install nexus (maven)
     
    Install nexus (maven)
    
    wget http://nexus.sonatype.org/downloads/nexus-webapp-1.4.0-bundle.zip
    unzip -e nexus-webapp-1.4.0-bundle.zip
    ln -s nexus-webapp-1.4.0 nexus
    
    # then start
    bin/jsw/linux-x86-64/nexus start
    
    # modify port
    vi conf/plexus.properties
    modify application-port.
    

    copy | embed

    0 comments - tagged in  posted by iamacnhero on Oct 31, 2009 at 2:18 a.m. EDT
  • Java comile process
     
    Java compile
    
    vi Test.java
    import java.util.*;
    public class Test {
            public static void main(String[] args) {
                   Map map = System.getProperties();
                    System.out.println(map);
            }
    }
    
    # javac Test.java
    # java Test     
    

    copy | embed

    0 comments - tagged in  posted by iamacnhero on Oct 30, 2009 at 10:48 p.m. EDT