March 17, 2012

Operating system information in java

To get the operation system information in java

Source code


public class OpertingSystemInfo
{
public static void main(String[] args)
{
String nameOS = "os.name";
String versionOS = "os.version";
String architectureOS = "os.arch";
System.out.println("\n The information about OS");
System.out.println("\nName of the OS: " +
System.getProperty(nameOS));
System.out.println("Version of the OS: " +
System.getProperty(versionOS));
System.out.println("Architecture of THe OS: " +
System.getProperty(architectureOS));
}
}


How to get my computer details in java


If you searching for a third partyAPI that helps you to get other information than above
SIGAR api is one of them

sigar class

read here

Thanking you....

How to print a triangle in java

This program shows you how to print out a triangle in java.



package com.sequestration.fuzzylogic.implementation;
import java.util.Scanner;


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

Scanner scanner=new Scanner(System.in);
System.out.println("(Triangle Triangle)\n\n Enter an odd number: ");
int n=scanner.nextInt();
int[] a={78,73,74,69,83,72};
System.out.print("\t coded by :");
for(int i=0;i<=5;i++)
System.out.print((char)a[i]);
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
if((n%2)==1)
{
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
{
if((i==j)||((i+j)==(n+1)))
{
System.out.print(i+" ");
}else
{
System.out.print(" ");
}
}
System.out.println();
}

}
else
System.out.println("\t"+ n+ " is not a odd number \n\t choose "+(n+1)+" or "+(n-1));
}

}



Thanking you....

Audio chatting example in java

Audio chatting example in java

The program show a small demonstration of how Audio chat works in java. Click on the link to get the code. Two or more user can audio chat with the application. The program is developed using networking and java packages.
 

Program


Please try audio chatting program... dont forget to post your feed back.

did you ever tried two way chatting using java code. Here we use socket communication, thread for this program. click the below link to download the code. javablzy

Thanking you....


you can mail me to belazy1987atgmaildotcom.

Bi direction java networking chat application source code

Two way Chatting example in java using tcp socket


Java Chat application using TCP java socket programing example.  The program communicates with other through port number 3000 and 2000, developer can change this to what ever they want. avoid using first 1024 reserved ports. The program search the other system by its computer name, so please provide the name correctly. The program is so simple, easy to understand. The interface here used is to create two variable for storing port number.  And explaining the code:  Getting user input as string, converting them to bytes, storing it in a variable then sent through datagram socket as datagram packets through a port number specified. On the other hand the program keep on listening to that port number, if a request comes through that socket , it will receive it as byte convert to string the display the message. JAVA thread is used for listening


System 1

/**
Chatting example in java
javabelazy
*/



import java.io.*;
import java.net.*;
interface pen{
final int cp=3000;
final int sp=2000;
}
public class Cli5 implements pen{
public static void main(String[] args)throws Exception {

byte buf1[]=new byte[1024];
DatagramSocket d=new DatagramSocket();
DataInputStream b=new DataInputStream(System.in);
tchat t=new tchat();
t.start();
System.out.println("Chatting programme");
while(true){
String s=b.readLine();
buf1=s.getBytes();
d.send(new DatagramPacket(buf1,s.length(),InetAddress.getByName("Redmi"),cp));
if(s.equals("end"))
System.exit(0);

}
}
}
class tchat extends Thread implements pen{
tchat() {}
byte buf2[]=new byte[1024];
public void run(){
try{
DatagramSocket dr=new DatagramSocket(sp);
while(true){
DatagramPacket p=new DatagramPacket(buf2,buf2.length);
dr.receive(p);
String s =new String(p.getData(),0,p.getLength());
System.out.println("Server :"+s);
System.out.println("Client :");
if(s.equals("end"))
System.exit(0);
}
}
catch(Exception e)
{}
}


}

-----------------------------------------------------------------------------

System 2



How to create a bi directional chat application using java  networking

/**
Chatting example in java
javabelazy
*/



import java.io.*;
import java.net.*;
interface pen{
final int cp=2000;
final int sp=3000;
}
public class Cli4 implements pen{
public static void main(String[] args)throws Exception {

byte buf1[]=new byte[1024];
DatagramSocket d=new DatagramSocket();
DataInputStream b=new DataInputStream(System.in);
tchat t=new tchat();
t.start();
System.out.println("Chatting program");
while(true){
String s=b.readLine();
buf1=s.getBytes();
d.send(new DatagramPacket(buf1,s.length(),InetAddress.getByName("Watsapp"),cp));
if(s.equals("end"))
System.exit(0);

}
}
}
class tchat extends Thread implements pen{
tchat() {}
byte buf2[]=new byte[1024];
public void run(){
try{
DatagramSocket dr=new DatagramSocket(sp);
while(true){
DatagramPacket p=new DatagramPacket(buf2,buf2.length);
dr.receive(p);
String s =new String(p.getData(),0,p.getLength());
System.out.println("client :"+s);

if(s.equals("end"))
System.exit(0);
}
}
catch(Exception e)
{}
}


}
The chat application currently work on your intranetwork

Thanking you....

March 13, 2012

Java program to play sound file

This is a small example in java to show how media files works. copy paste the code in some ide or also you can use notepad to run the application. Click on the  Program to get the source code.
Thanking you....

Download sound files

Access restriction: The type NetworkInterface is not accessible

How to solve the following error in eclipse IDE ?

Access restriction: The type Network Interface is not accessible


Eclipse IDE



Dear Friends, Hope any one had gone through this error once in your life if you had worked on  any java networking projects  or programs

This post will help you to clear the error " Access restriction : The type network interface is not accessible" which happens while java developers works on eclipse.


Solution

Do the following in eclipse (steps to do)

Go to

windows

tab in eclipse,

Navigate to

Preferences


then

Java


then

compiler


choose

Errors/Warning


you can see

Deprecated and restricted A P I


in that

Forbidden Reference ( access rules)


change this to warning




If you found the post is useful Or have any mistakes please provide your feedback through comments.

Thanking you....

Scientific calculator in java

Scientific Calculator source code developed in java swing

Description : A simple Java Swing Calculator, Example for setBounds in java, Example for actionListener interface. Here i tried to Implement a simple Java Calculator. copy and paste the code to a notepad. compile the program with javac and run using command prompt java calculator




Author +belazy

/* this is a program to make a

calculator

using java program */

/* For SNR Sons Lab exam ...
 * @author belazy
 */




import java.awt.*;
import java.awt.event.*;

public class

Calculator

extends Frame implements ActionListener

{
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,ex,p1,eq,min,div,mul,cl,back;
TextField t;
int n,i;
int n1,n2;
int ans;

Calculator()
{
this.setLayout(null);
b0=new Button(" 0 ");
b1=new Button(" 1 ");
b2=new Button(" 2 ");
b3=new Button(" 3 ");
b4=new Button(" 4 ");
b5=new Button(" 5 ");
b6=new Button(" 6 ");
b7=new Button(" 7 ");
b8=new Button(" 8 ");
b9=new Button(" 9 ");
ex=new Button("exit");
p1=new Button("+");
min=new Button("-");
mul=new Button("X");
div=new Button("/");

eq=new Button("=");
cl=new Button("AC");
back=new Button("clear");

t=new TextField(40);

b0.setBounds(130,190,30,30);
b1.setBounds(100,100,30,30);
b2.setBounds(130,100,30,30);
b3.setBounds(160,100,30,30);
b4.setBounds(100,130,30,30);
b5.setBounds(130,130,30,30);
b6.setBounds(160,130,30,30);
b7.setBounds(100,160,30,30);
b8.setBounds(130,160,30,30);
b9.setBounds(160,160,30,30);
ex.setBounds(100,190,30,30);
p1.setBounds(190,100,30,30);
min.setBounds(190,130,30,30);
mul.setBounds(190,160,30,30);
div.setBounds(190,190,30,30);
eq.setBounds(160,190,30,30);
cl.setBounds(220,100,30,30);
back.setBounds(220,130,30,30);

t.setBounds(100,60,150,30);



this.add(b0);
this.add(b1);
this.add(b2);
this.add(b3);
this.add(b4);
this.add(b5);
this.add(b6);
this.add(b7);
this.add(b8);
this.add(b9);

this.add(ex);
this.add(p1);
this.add(mul);
this.add(min);
this.add(div);
this.add(eq);
this.add(cl);
this.add(back);

this.add(t);

b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);

ex.addActionListener(this);
p1.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
min.addActionListener(this);
eq.addActionListener(this);
cl.addActionListener(this);
back.addActionListener(this);
}


public void actionPerformed(ActionEvent ae)

{

if(ae.getSource()==ex)
{
System.exit(0);
}

if(ae.getSource()==p1) // while pressing plus
{

n1=Integer.parseInt(t.getText());
i=1; // this expression is for equals
t.setText("0");

}

if(ae.getSource()==min) // while pressing minus
{

n1=Integer.parseInt(t.getText());
i=2;
t.setText("0");

}

if(ae.getSource()==mul) // while pressing multiplication
{

n1=Integer.parseInt(t.getText());
i=3;
t.setText("0");

}

if(ae.getSource()==div) // while pressing division
{

n1=Integer.parseInt(t.getText());
i=4;
t.setText("0");

}
if(ae.getSource()==cl) // while pressing all clear
{

//n1=Integer.parseInt(t.getText());
//i=4;
t.setText("0");

}

if (ae.getSource()==back) // for back space
{
//if (!(t.getText().equals("0") && t.getText().length() > 1)

t.setText(t.getText().substring(0,t.getText().length()-1));

}




if(ae.getSource()==b0)
{
if(t.getText().equals("0"))
t.setText("0");
else
t.setText(t.getText() +"0");
}

if(ae.getSource()==b1)
{
if(t.getText().equals("0"))
t.setText("1");
else
t.setText(t.getText() +"1");
}

if(ae.getSource()==b2)
{
if(t.getText().equals("0"))
t.setText("2");
else
t.setText(t.getText() +"2");
}


if(ae.getSource()==b3)
{
if(t.getText().equals("0"))
t.setText("3");
else
t.setText(t.getText() +"3");
}

if(ae.getSource()==b4)
{
if(t.getText().equals("0"))
t.setText("4");
else
t.setText(t.getText() +"4");
}

if(ae.getSource()==b5)
{
if(t.getText().equals("0"))
t.setText("5");
else
t.setText(t.getText() +"5");
}

if(ae.getSource()==b6)
{
if(t.getText().equals("0"))
t.setText("6");
else
t.setText(t.getText() +"6");
}

if(ae.getSource()==b7)
{
if(t.getText().equals("0"))
t.setText("7");
else
t.setText(t.getText() +"7");
}

if(ae.getSource()==b8)
{
if(t.getText().equals("0"))
t.setText("8");
else
t.setText(t.getText() +"8");
}

if(ae.getSource()==b9)
{
if(t.getText().equals("0"))
t.setText("9");
else
t.setText(t.getText() +"9");
}
System.out.println(t.getText());

if(ae.getSource()==eq) //while pressing equals
{
n2=Integer.parseInt(t.getText());




switch(i)
{
case 1:
{

ans=n1+n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;
}
break;

case 2:
{

ans=n1-n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;

}
break;

case 3:
{

ans=n1*n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;

}
break;

case 4:
{

ans=n1/n2;
t.setText(Integer.toString(ans));
n1=0;
n2=0;

}
break;

}

}



}



public static void main(String free[])
{

Calculator c= new Calculator();
c.setTitle("

simple java calculator source code

");
c.setSize(600,600);
c.setVisible(true);

}

}


Hi Friends,

           This is my first program in java. so please try it out and find the issues. Thanks to ravi, vishnu , madhu, bithesh and nithin for support

/* contact me at belazy1987atgmail.com */

Another Program send by my friend,

simple java calculator source code

import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
//<applet code=Calculator height=300 width=200></applet>
public class Calculator extends JApplet {
public void init() {
CalculatorPanel calc=new CalculatorPanel();
getContentPane().add(calc);
}
}

class CalculatorPanel extends JPanel implements ActionListener {
JButton
n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;
static JTextField result=new JTextField("0",45);
static String lastCommand=null;
JOptionPane p=new JOptionPane();
double preRes=0,secVal=0,res;

private static void assign(String no)
{
if((result.getText()).equals("0"))
result.setText(no);
else if(lastCommand=="=")
{
result.setText(no);
lastCommand=null;
}
else
result.setText(result.getText()+no);
}

public CalculatorPanel() {
setLayout(new BorderLayout());
result.setEditable(false);
result.setSize(300,200);
add(result,BorderLayout.NORTH);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(4,4));

n7=new JButton("7");
panel.add(n7);
n7.addActionListener(this);
n8=new JButton("8");
panel.add(n8);
n8.addActionListener(this);
n9=new JButton("9");
panel.add(n9);
n9.addActionListener(this);
div=new JButton("/");
panel.add(div);
div.addActionListener(this);

n4=new JButton("4");
panel.add(n4);
n4.addActionListener(this);
n5=new JButton("5");
panel.add(n5);
n5.addActionListener(this);
n6=new JButton("6");
panel.add(n6);
n6.addActionListener(this);
mul=new JButton("*");
panel.add(mul);
mul.addActionListener(this);

n1=new JButton("1");
panel.add(n1);
n1.addActionListener(this);
n2=new JButton("2");
panel.add(n2);
n2.addActionListener(this);
n3=new JButton("3");
panel.add(n3);
n3.addActionListener(this);
minus=new JButton("-");
panel.add(minus);
minus.addActionListener(this);

dot=new JButton(".");
panel.add(dot);
dot.addActionListener(this);
n0=new JButton("0");
panel.add(n0);
n0.addActionListener(this);
equal=new JButton("=");
panel.add(equal);
equal.addActionListener(this);
plus=new JButton("+");
panel.add(plus);
plus.addActionListener(this);
add(panel,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==n1) assign("1");
else if(ae.getSource()==n2) assign("2");
else if(ae.getSource()==n3) assign("3");
else if(ae.getSource()==n4) assign("4");
else if(ae.getSource()==n5) assign("5");
else if(ae.getSource()==n6) assign("6");
else if(ae.getSource()==n7) assign("7");
else if(ae.getSource()==n8) assign("8");
else if(ae.getSource()==n9) assign("9");
else if(ae.getSource()==n0) assign("0");
else if(ae.getSource()==dot)
{
if(((result.getText()).indexOf("."))==-1)
result.setText(result.getText()+".");
}
else if(ae.getSource()==minus)
{
preRes=Double.parseDouble(result.getText());
lastCommand="-";
result.setText("0");
}
else if(ae.getSource()==div)
{
preRes=Double.parseDouble(result.getText());
lastCommand="/";
result.setText("0");
}
else if(ae.getSource()==equal)
{
secVal=Double.parseDouble(result.getText());
if(lastCommand.equals("/"))
res=preRes/secVal;
else if(lastCommand.equals("*"))
res=preRes*secVal;
else if(lastCommand.equals("-"))
res=preRes-secVal;
else if(lastCommand.equals("+"))
res=preRes+secVal;
result.setText(" "+res);
lastCommand="=";
}
else if(ae.getSource()==mul)
{
preRes=Double.parseDouble(result.getText());
lastCommand="*";
result.setText("0");
}
else if(ae.getSource()==plus)
{
preRes=Double.parseDouble(result.getText());
lastCommand="+";
result.setText("0");
}

}
}


The above is an example code for java swing scientific calculator, copy and paste it as calculator.java, compile the code and run it.

Please provide your feedback.

Java applet tutorial

  1. How to run java applet in web browser

Similar posts

  1. calendar using java swing
  2. compare two images in java
  3. Developing an swing IDE in java
  4. Developing an Billing system in java using swing
  5. Chatting application in java
  6. Algorithm to find missing number in a sequence with minimum complexity
< Thanking you....

March 09, 2012

Reflection example in java


Example for reflection in java

Reflection in java makes it possible to inspect and call classes, methods, attributes etc dynamically at run time. Reflection is not possible in other programming languages such as c++.

Here i wrote a simple java program to know how reflection works.

StudentModel.java


/**
* JavaBelazy Java source code download
*/

/**
* @author +belazy
*
*/
public class StudentModel {

private String name = "javasourcecode";

private int id = 622;

private boolean isPassed = false;

//private double totalMark = 788;



public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public boolean isPassed() {
return isPassed;
}

public void setPassed(boolean isPassed) {
this.isPassed = isPassed;
}





}




March 08, 2012

how to detect broadcast ip in java


how to detect broadcast ip in java

 How to detect Denial of Service attack

The project need jpcap.

import jpcap.*;
import jpcap.packet.ARPPacket;
import jpcap.packet.Packet;

class Tcpdump implements PacketReceiver {
public void receivePacket(Packet packet) {
if (packet instanceof ARPPacket) {
System.out.println(packet);
ARPPacket arp=(ARPPacket)packet;
// System.out.println(arp.hardtype=ARPPacket.HARDTYPE_ETHER);
// System.out.println(arp.prototype=ARPPacket.PROTOTYPE_IP);
// System.out.println(arp.prototype=ARPPacket.PROTOTYPE_IP);
// System.out.println(arp.operation=ARPPacket.ARP_REQUEST);
System.out.println("sender hardware address ----->" +arp.getSenderHardwareAddress());
System.out.println("Sender protocol address ----->"+arp.getSenderProtocolAddress());
System.out.println("target hardware address ----->"+arp.getTargetHardwareAddress());
System.out.println("target protocol address ----->"+arp.getTargetProtocolAddress());
System.out.println(" arp reply (short) ----->"+ arp.ARP_REPLY);
System.out.println(" arp request (short) ----->"+arp.ARP_REQUEST);
// System.out.println(" arp caplen (int) ----->"+arp.caplen);
// System.out.println(" arp hardtype (short) ----->"+arp.hardtype);
// System.out.println(" arp hlen (short) ------>"+arp.hlen);
// System.out.println(" arp packetlength (int) ------>"+arp.len);
// System.out.println(" arp operation (short) ----->"+arp.operation);
// System.out.println(" arp plen (short) ------>"+arp.plen);
// System.out.println(" arp prototype (short) ------>"+arp.prototype);
// System.out.println(" arp second (long) ------>"+arp.sec);
// System.out.println(" arp usec (long) ------>"+arp.usec);
// System.out.println(" arp hashcode (int) ------>"+arp.hashCode());
// System.out.println(" arp to string ------>"+ arp.toString());
// System.out.println(" arp hardtype ether(short) ------>"+arp.HARDTYPE_ETHER);
// System.out.println(" arp frame relay (short) ------>"+arp.HARDTYPE_FRAMERELAY);
// System.out.println(" arp hardtype ieee802(short) ---->"+arp.HARDTYPE_IEEE802);
// System.out.println(" arp inv reply (short)----->"+arp.INV_REPLY);
// System.out.println(" arp inv request (short)----->"+arp.INV_REQUEST);
// System.out.println(" arp prototype ip (short) ---->"+arp.PROTOTYPE_IP);
// System.out.println(" arp rarp reply (short)----->"+arp.RARP_REPLY);
// System.out.println(" arp rarp request (short)----->"+ arp.RARP_REQUEST);
// System.out.println(" arp data (byte[])---->"+arp.data);
// System.out.println(" arp datalink ( datalink)----->"+arp.datalink);
// System.out.println(" arp header (byte[]) ----->"+arp.header);
// System.out.println(" arp sender hardaddress (byte[])---->"+arp.sender_hardaddr);
// System.out.println(" arp sender proto address (byte[]) ---->"+arp.sender_protoaddr);
// System.out.println(" arp target hard address (byte[])---->"+arp.target_hardaddr);
// System.out.println(" arp target proto address (byte[])----->"+arp.target_protoaddr);
// System.out.println(" arp get class (class) ----->"+arp.getClass());
// System.out.println(" arp end of file ---->"+arp.EOF);
System.out.println(" ******************************************************* ");
String s=arp.getSenderProtocolAddress().toString();
System.out.println("string" +s);
if(s.endsWith("1"))
{
int l=s.length();
String sub=s.substring(1,l);
System.out.println(" sub string(ipaddr)"+sub);
}

System.out.println(" ******************************************************* ");



// arp.hlen=6;
// arp.plen=4;

}
}

public static void main(String[] args) throws Exception {
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
// if(args.length<1){
// System.out.println("usage: java Tcpdump

Facebook comments