Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Wednesday, 28 September 2016

Shut down all PCs(Windows 7) in LAN using Java from Linux

Shop on Amazon                                                                 Google Apps for Work


I have made a web application that shutdown all PCs in LAN in one click. I am working in a computer lab and shutting down all PCs daily is very irritating for me. So, I decided to develop an application that search all PCs, which are turned on and shut them all.

My environment is:- (prerequisite)
  1. I use Ubuntu 14.04 LTS on my PCs.
  2. All LAN PCs are using windows 7 Operating System.
  3. Java should be installed on server.
Since this application is client-server based, so some setting and software will be required on both side.

1. On client side:-

To shut down PC from remote location you require some registry edit in Windows 7 system.
Open command prompt as administrator and run following command:-

reg add HKLM\SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\POLICIES\SYSTEM /V LocalAccountTokenFilterPolicy /t REG_DWORD /d 1

OR

Open the registry editor by going to Start and typing in regedit. Now navigate to the following key:
HKEY_LOCAL_MACHINE – SOFTWARE – Microsoft – Windows – CurrentVersion – Policies – System
Right-click on System on the left hand side and choose New – DWORD (32-bit) Value.


Now your client machine is ready to shut down remotely.

2. On server side:-

In my case, I am running Ubuntu 14.04 and I have installed Java version “1.8.0_74” of Oracle.

To shut down windows PC from Linux machine, we required to install a package samba-common. We can install this by typing on terminal

sudo apt-get install samba-common

After installing this package run following command to check that everything is working:-

net rpc shutdown -I ipaddress -U username%password

note:- If server is windows machine, you can find the windows command to shutdown a remote PC.

3. A Java program

If remote shutdown is successful after above set up, we require a Java program to make it automatic.

To shut down a single machine, Java code is given below:-

import java.io.*;

String com="net rpc shutdown -I";
String ip="192.168.25.52";
String cred="-U admin%logitech";
String command=com+" " +ip+" "+cred;
try {
Process process = Runtime.getRuntime().exec(command);
System.out.println("the output stream is "+process.getOutputStream());
BufferedReader reader=new BufferedReader( new InputStreamReader(process.getInputStream()));
String s;
while ((s = reader.readLine()) != null){
System.out.println("The inout stream is " + s);
}
} catch (IOException e) {
e.printStackTrace();
}


If you want to shutdown all LAN PCs, code can be:-

try{
String com="net rpc shutdown -I";
String cred="-U admin%logitech";

byte[] ip = {(byte)192, (byte)168, 25, 1}; // for 192.168.0.x addresses and starting ip address
for (int i = 1; i <=5; i++) //this will shut 1 to 5 pcs in ip range, you can limit the loop as many pcs you want to shut
{

ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
String ipad=address.toString().substring(1);
String command=com+" " +ipad+" "+cred;
System.out.println(command);
Process process = Runtime.getRuntime().exec(command);
System.out.println("the output stream is "+process.getOutputStream());
BufferedReader reader=new BufferedReader( new InputStreamReader(process.getInputStream()));
String s;
while ((s = reader.readLine()) != null){
System.out.println("The inout stream is " + s);
}
}

}
catch(Exception e)
{
}


Either you can implement this code on a java program or in JSP/Servlet to shut LAN PCs.

Sunday, 4 September 2016

Send data with hyperlink via URL in JSP/JAVA

We all know that we send data to another page using form or using session variables. But, some time we have to send data to another page using hyperlink. Suppose, we have fetched data from database and made id as hyperlink and clicking on it we do some operation like edit, delete or some thing else.

Let's see how we can do it in simple JSP page.



Here you can see update and delete is associated with each record in the form of hyperlink.

Code is-

<td><a href='updaterecord.jsp?srno=<%=rs.getInt(1)%>' target='blank'>Update</td>
<td><a href='deleterecord.jsp?srno=<%=rs.getInt(1)%>' target='blank'>Delete</td>

Using hyperlink we send the srno(primary key- fetched by rs.getInt(1)) of corresponding record to updaterecord.jsp and deleterecord.jsp pages with the help of srno variable.

When we click on update and delete hyperlink the rs.getInt(1) data sent to another page using srno variable via URL. See the URL after clicking on link


 Now on updaterecord.jsp page we can receive srno variable  value using

                  int search=Integer.parseInt(request.getParameter("srno"));

After getting srno of that particular record, we can do anything related to this record.

Saturday, 3 September 2016

Make JFrame unmovable, unclosable, unminimizable, unresizable & unmaximizable in Java Swing

Suppose, you have to develop a application which requires desktop locking- means user can not use the system if you don't allow to use it. For such kind of application we require that (I assume that it is a swing application) the JFrame should not be closed, maximized, minimized or movable.

To do so, we require remove title bar along with close, maximize and minimize buttons (windows buttons), so that JFrame could not be closed, moved, maximized or minimized

setUndecorated(true);    //remove title bar

setResizable(false);      //make frame static

setType(javax.swing.JFrame.Type.UTILITY);     //Jframe will not show in task bar

setAlwaysOnTop(true);       //set always on top if we don't close it using any action


Dynamically create rows, checkboxes, buttons in JSP/JAVA

Generally we create rows, check boxes, buttons, hyperlink statically. But when we want it to create dynamically or when we have to associate it with row fetched from database and we don't know the exact result has to come.

If we want to create fixed number of elements dynamically, obviously we use loop like that

<table>
<%
int i=0;
while(i<=10)
{
%>
              <tr><td>your data</td></tr>   //it prints 10 table row
              <input  type="search"  />        // it prints 10 textfield
              <input type="submit" value="Search" />   // it prints 10 buttons
              ..... and so on 
<%
i++;
}
%>
</table>


If we get data from database and want these elements to associate with this then

just change the loop

while(rs.next())
{
// put your elements according to you//
}



Sunday, 24 July 2016

Emergence of Java, is not just another programming language.

Programming is about managing complexity: the complexity of the problem you want to solve, laid upon the complexity of the machine in which it is solved. Because of this complexity, most of our programming projects fail. And yet, of all the programming languages of which people aware, none of them have gone all-out and decided that their main design goal would be to conquer the complexity of developing and maintaining programs.[1] Of course, many language design decisions were made with complexity in mind, but at some point there were always some other issues that were considered essential to be added into the mix. Inevitably, those other issues are what cause programmers to eventually “hit the wall” with that language. For example, C++ had to be backwards-compatible with C (to allow easy migration for C programmers), as well as efficient. Those are both very useful goals and account for much of the success of C++, but they also expose extra complexity that prevents some projects from being finished (certainly, you can blame programmers and management, but if a language can help by catching your mistakes, why shouldn’t it?). As another example, Visual BASIC (VB) was tied to BASIC, which wasn’t really designed to be an extensible language, so all the extensions piled upon VB have produced some truly horrible and unmaintainable syntax. Perl is backwards-compatible with Awk, Sed, Grep, and other Unix tools it was meant to replace, and as a result is often accused of producing “write-only code” (that is, after a few months you can’t read it). On the other hand, C++, VB, Perl, and other languages like Smalltalk had some of their design efforts focused on the issue of complexity and as a result are remarkably successful in solving certain types of problems. 

What has impressed people most as they have come to understand Java is that somewhere in the mix of Sun’s design objectives, it appears that there was the goal of reducing complexity for the programmer. As if to say “we care about reducing the time and difficulty of producing robust code.” In the early days, this goal resulted in code that didn’t run very fast (although there have been many promises made about how quickly Java will someday run) but it has indeed produced amazing reductions in development time; half or less of the time that it takes to create an equivalent C++ program. This result alone can save incredible amounts of time and money, but Java doesn’t stop there. It goes on to wrap many of the complex tasks that have become important, such as multithreading and network programming, in language features or libraries that can at times make those tasks easy. And finally, it tackles some really big complexity problems: cross-platform programs, dynamic code changes, and even security, each of which can fit on your complexity spectrum anywhere from “impediment” to “show-stopper.” So despite the performance problems we’ve seen, the promise of Java is tremendous: it can make us significantly more productive programmers. 

One of the places people see the greatest impact for this is on the Web. Network programming has always been hard, and Java makes it easy (and the Java language designers are working on making it even easier). Network programming is how we talk to each other more effectively and cheaper than we ever have with telephones (email alone has revolutionized many businesses). As we talk to each other more, amazing things begin to happen, possibly more amazing even than the promise of genetic engineering.

In all ways—creating the programs, working in teams to create the programs, building user interfaces so the programs can communicate with the user, running the programs on different types of machines, and easily writing programs that communicate across the Internet—Java increases the communication bandwidth between people. People think that the results of the communication revolution may not be seen from the effects of moving large quantities of bits around; we shall see the true revolution because we will all be able to talk to each other more easily: one-onone, but also in groups and, as a planet. People have heard it suggested that the next revolution is the formation of a kind of global mind that results from enough people and enough interconnectedness. Java may or may not be the tool that foments that revolution, but at least the possibility has made me feel like people are doing something meaningful by attempting to teach the language. 



Inspired by Bruce Eckel.

Sunday, 19 July 2015

Evolution of Java & Version History

My title

Evolution of Java & Version History

Since, Java is known for the language of Internet. But originally it was not developed for Internet. Its primary purpose was to develop a language that would the platform-independent (i.e. architecture neutral). Java was invented to create software that could be embedded in consumer electronic devices, such as microwave ovens and remote controls.
Java was developed in 18 months by James Gosling, Patrick Naughton and et all at Sun Microsystem in 1991. It was initially called "Oak". But, come in market in 1995 by the name "Java".
So, what was the need of platform independent language?
Contrary to Java, most of languages require specific architecture to run. However it is possible that any language can compile on any architecture, but it requires a compiler that is targeted for that CPU or architecture.
Was the java team only considered about portable programs?
It is not true that only java team considered about a portable program. At early stage of programming, most of the programmers learn that portable programs are desirable. The computer world at that time was divided into three competing camps Intel, Macintosh & UNIX. Programmers are bounded in their boundaries hence; urgent need of portable program was reduced.
But after invent of World Wide Web and presence of diverse computers, operating systems & CPU, need of portable program one again returned with a vengeance. And low-priority problem had become the high-profile necessity.
About Java
Many people think that, Java is enhanced version of C++ because of similarity between them. But expert think that it would be a large mistake. Java is differing practically & philosophically from C++. While it is true that it was influenced by C++.
Java is a high-level programming language as well as a platform.
The Java programming language is designed to meet the challenges of application development in the context of heterogeneous, network-wide distributed environments. Paramount among these challenges is secure delivery of applications that consume the minimum of system resources, can run on any hardware and software platform, and can be extended dynamically.
The Java programming language originated as part of a research project to develop advanced software for a wide variety of network devices and embedded systems. The goal was to develop a small, reliable, portable, distributed, real-time operating platform. When the project started, C++ was the language of choice. But over time the difficulties encountered with C++ grew to the point where the problems could best be addressed by creating an entirely new language platform. Design and architecture decisions drew from a variety of languages such as Eiffel, SmallTalk, Objective C, and Cedar/Mesa. The result is a language platform that has proven ideal for developing secure, distributed, network-based end-user applications in environments ranging from network-embedded devices to the World-Wide Web and the desktop.
[James Gosling, 1996]
Java Versions
1995 – JDK alpha & beta was highly unstable.
23 Jan 1996 – JDK 1.0 Originally called Oak.
19 Feb 1997 – JDK 1.1 JIT compiler released for Microsoft Windows Platform.
8 Dec 1998 – J2SE 1.2 Codename Playground, from here to JDK 1.5 it was re-branded as Java 2     followed by version like J2SE 1.2.
8 May 2000 – J2SE 1.3 Codename Kestrel with HotSpot.
6 Feb 2002- J2SE 1.4 Codename Merlin.
30 Sep 2004- J2SE 5.0 Codename Tiger, Originally numbered 1.5. The number was changed to "better reflect the level of maturity, stability, scalability and security of the J2SE."
11 Dec 2006 – JSE 6 Codename Mustang, Sun replaced the name J2SE to JSE and Sun was take over by Oracle but internal numbering for developers will remain as 1.6.0.
28 Jul 2011 – JSE 7 Codename Dolphin. Java 7 was the default version to download on java.com from April 2012 until Java 8 was released.
18 Mar 2014 – JSE 8 Language-level support for lambda expression under Project Lambda and default methods (virtual extension methods) which make multiple inheritance possible in Java. And implementation of multiple inheritance more precisely. It also allows embed JavaScript code within applications. Java 8 has been the default version to download from the official website.
22 Sep 2016 – JSE 9 might be released.

References:-
1.https://docs.oracle.com/javase/tutorial/getStarted/intro/definition.html.
2.Java 2 The Complete Reference by Herbert Schildt.
 3.The Java Language Environment , a white paper written by James Gosling and Henry McGilton.
4.https://en.wikipedia.org/wiki/Java_version_history#cite_note-JDK7-96

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...