Tuesday 29 December 2015

Run Javascript from Java.

This is an interface available part of java api. This should be implemented separately for every scripting languages. For javascript in the Oracle JDK (from 1.6) by default an implementation is available. Apache commons provides a project Jakarta Bean Scripting Framework (BSF) which gives implementation for a several set of scripting languages like Python, TCL, NetRexx including javascript and lot more.

here is code Example:

import javax.script.*;

public class JavaJavaScript {
 public static void main(String args[]) throws ScriptException {
     ScriptEngineManager manager = new ScriptEngineManager();
     ScriptEngine engine = manager.getEngineByName("javascript");

     engine.eval("var x = 10;");
     engine.eval("var y = 20;");
     engine.eval("var z = x + y;");
     engine.eval("print (z);");
 }
}

Tuesday 22 December 2015

bootstrap login Window (one Page application)

<html lang="en">

<head>

<!-- Latest compiled and minified CSS -->
 <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- jQuery library -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->

<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
 <meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>

<div class="container">

<div class="col-sm-12">

<a href="#"  class="btn btn-info btn-xs" data-toggle="modal" data-target="#myModal" >Edit</a>

</div>


<div class="modal fade" id="myModal" role="dialog">

    <div class="modal-dialog">

    

      <!-- Modal content-->

      <div class="modal-content">

        <div class="modal-header">

          <button type="button" class="close" data-dismiss="modal">&times;</button>

          <h4 class="modal-title">Lgin</h4>

        </div>

        <div class="modal-body">

        <!-- Your working area -->

        <div class="row">

       <div class="col-sm-3">&nbsp;</div>

       <div class="col-sm-3">UserName:</div>

       <div class="col-sm-3"><input type="text"  id="UserName"  /></div>

       <div class="col-sm-3">&nbsp;</div>

        </div>

        <div class="row">

       <div class="col-sm-3">&nbsp;</div>

       <div class="col-sm-3">Password:</div>

       <div class="col-sm-3"><input type="password"  id="password" /></div>

       <div class="col-sm-3">&nbsp;</div>

        </div>

        </div>

 

        <!-- Your working area -->

                </div>

        <div class="modal-footer">

          <button type="button" class="btn btn-default btn-xs" data-dismiss="modal" id="cls">Close</button>

           <button type="button" class="btn btn-info btn-xs"  id="lgin">Login</button>

        </div>

      </div>

      

    </div>

  </div>

  </div>

</body>

</html>

It looks as....

Sunday 20 December 2015

How Capture Sound via pc microphone in java?

       

import javax.sound.sampled.*;
import java.io.*;


public class JavaSoundRecorder {
 // record duration, in milliseconds
 static final long RECORD_TIME = 6000; // 1 minute

 // path of the wav file
 File wavFile = new File("RecordAudio.wav");

 // format of audio file
 AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;

 // the line from which audio data is captured
 TargetDataLine line;

 /**
  * Defines an audio format
  */
 AudioFormat getAudioFormat() {
  float sampleRate = 8000.0F;//8000,11025,16000,22050,44100
  int sampleSizeInBits = 16;
  int channels =1;
  boolean signed = true;
  boolean bigEndian = true;
  AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,channels, signed, bigEndian);
  return format;
 }

 /**
  * Captures the sound and record into a WAV file
  */
 void start() {
  try {
   AudioFormat format = getAudioFormat();
   DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

   // checks if system supports the data line
   if (!AudioSystem.isLineSupported(info)) {
    System.out.println("Line not supported");
    System.exit(0);
   }
   line = (TargetDataLine) AudioSystem.getLine(info);
   line.open(format);
   line.start(); // start capturing

   System.out.println("Start capturing...");

   AudioInputStream ais = new AudioInputStream(line);

   System.out.println("Start recording...");

   // start recording
   AudioSystem.write(ais, fileType, wavFile);

  } catch (LineUnavailableException ex) {
   ex.printStackTrace();
  } catch (IOException ioe) {
   ioe.printStackTrace();
  }
 }

 /**
  * Closes the target data line to finish capturing and recording
  */
 void finish() {
  line.stop();
  line.close();
  System.out.println("Finished");
 }

 /**
  * Entry to run the program
  */
 public static void main(String[] args) {
  final JavaSoundRecorder recorder = new JavaSoundRecorder();

  // creates a new thread that waits for a specified
  // of time before stopping
  Thread stopper = new Thread(new Runnable() {
   public void run() {
    try {
     Thread.sleep(RECORD_TIME);
    } catch (InterruptedException ex) {
     ex.printStackTrace();
    }
    recorder.finish();
   }
  });

  stopper.start();

  // start recording
  recorder.start();
 }
}

 

Friday 11 December 2015

How to lock your pc using bat file ?

Open notepad and write flowing code 

rundll32.exe user32.dll, LockWorkStation

and save it to  lock.bat .After saving the file  click on file to execute it.It will luck your computer.

Thursday 3 December 2015

4 digit Random Number generation in java.


import java.util.*;
public class test {
   public static void main(String[] args) {
   creating UUID      
   UUID uid = new UUID();     
   HashSet hs=new HashSet();
     
   // checking the value of random UUID

  for(int i=1;i<1000;i++){
   //System.out.println("Random UUID value: "+UUID.randomUUID().toString());   
    
    Random r=new Random();
    String st[]=new Integer(r.nextInt(i)).toString().split("");
   String s[]=UUID.randomUUID().toString().split("-");
   System.out.println(UUID.randomUUID());
   System.out.println(s[1].replaceAll("[^0-9]","1"+st[0])); 
   hs.add(s[1].replaceAll("[^0-9]","1"+st[0]));
  
   }
       
  System.out.println(hs.size()+".............................");
}
}