Pages

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, September 23, 2014

Openssl create SSL certificate

What is SSL?


SSL (Secure Sockets Layer) is a standard security technology for establishing an encrypted link between a server and a client—typically a web server (website) and a browser or a mail server and a mail client (e.g., Outlook, Thunderbird).

So to make our server support SSL, we need to create SSL certificate for our server.

SSL certificate can be either Self-signed or signed by some authority like OpenCA.

How to create a SSL certificate?

To create a certificate we have to use a tool called openssl available for all operating systems.

And, follow these steps:

1. Create a private key using following command:


openssl genrsa -des3 -out server.key 1024

This will ask for a secure password for our key:

Generating RSA private key, 1024 bit long modulus
.......++++++
..............++++++
unable to write 'random state'
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
A server.key file will be created in the directory.

2. Remove Passphrase from Key

openssl genrsa -out server.key 1024

This will not ask for pass phrase every time it will be used.

3. Create a signing request (Certificate Signing Request):


openssl req -new -key server.key -out server.csr

This as for server information

Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:IN
State or Province Name (full name) [Some-State]:UP
Locality Name (eg, city) []:NOIDA
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Open Source
Organizational Unit Name (eg, section) []:JAVA
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:changeit
An optional company name []:localhost
A server.csr file will be created in the directory. This file can used to send to the signing authority or can be used to create a self signed certificate.

4. Create a Self signed certificate: 

Create a self signed certificate using private key:

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

This will create a server.crt file in the directory.


5. Add certificate to trusted certificates list in JAVA:

Create a DER format file of certificate:

openssl x509 -in server.crt -out server.der -outform DER

Adding certificate to trusted certificates using java keytool which is available in java bin directory:

keytool -import -keystore $JAVA_HOME/jre/lib/security/cacerts -file server.der -alias server


This will add our created certificate to JAVA trusted certificates list which is necessary for java based applications.

6. Configuring SSL Enabled Virtual Hosts

Now edit virtual host configuration to add following lines:

SSLEngine on

SSLCertificateFile /path/to/server.crt

SSLCertificateKeyFile path/to/server.key


Thursday, September 18, 2014

Set Java alternatives in Linux

There are several version of java which are in use. We can have different application written in different versions of java. So to use a particular version we have to set a preferable version of java which will be used by our system. We can set java alternatives using these two methods which i prefer:

1. Scan for alternatives and choose from them
2. Manually assign alternative

1. Scan for alternatives and choose from them


If you have installed two versions of java line Java 6 and Java 7. You must be having directories for these versions in your /usr/lib/jvm directory. Now to choose from alternative use following command:


#update-alternatives --config java

This will scan for available versions of java in your system and give option two choose from them:

There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status 
———————————————————— 
* 0 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 auto mode 
1 /usr/lib/jvm/jre1.7.0/jre/bin/java 3 manual mode  
Press enter to keep the current choice[*], or type selection number: 1

Now Java 7 will be set as preferable java version.
If you are using development environment, then you have to do same thing for javac:

#update-alternatives --config javac

2. Manually assign alternative

Now, if you want to use some java version which is not installed and you have downloaded its BIN version, in that case you can manually assign java version using following cammand:

#update-alternatives --install "/usr/bin/java" "java" "/home/user/jdk1.6_37/jre/bin/java" 1
#update-alternatives --set java /home/user/jdk1.6_37/jre/bin/java

and for javac,

#update-alternatives --install "/usr/bin/java" "java" "/home/user/jdk1.6_37/bin/javac" 1
#update-alternatives --set java /home/user/jdk1.6_37/bin/javac

Tuesday, September 9, 2014

How to use Syntaxhighlighter?

Edit your template's HTML code and add following code in the end of head tag in your template:

 
    
    
    
    
 

    
    
    
    
    
    
    
    
    
    
    
    
 

To use highlighter add pre tag around your code:

      
System.out.println("Hello!!");

Thursday, September 4, 2014

Install Java in Chrome, Chromium or Firefox in Ubuntu

To enable Java plugin in your Linux browsers, just copy these lines into a script, and run it!

JAVA_HOME=/usr/lib/jvm/jdk1.7.0  or the path where you have installed java

MOZILLA_HOME=~/.mozilla mkdir$MOZILLA_HOME/plugins

For 32-bit systems :


ln -s $JAVA_HOME/jre/lib/i386/libnpjp2.so $MOZILLA_HOME/plugins

For 64-bit systems:

ln -s $JAVA_HOME/jre/lib/amd64/libnpjp2.so $MOZILLA_HOME/plugins

For running a script in linux use following commands:

1. Open terminal.

2. Go to the directory where you have created script. (Note: your file must have extension .sh. For example: file_name.sh )

3. Write following commands:  chmod +x <file_name>.sh

4. Now write ./<file_name>.sh

5. Restart your browser.

Install Oracle Sun JDK in Ubuntu and set JAVA_HOME

To install JDK from Oracle use following steps:

1. Download JDK from Oracle web site:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

2. Extract file will be of .bin format. For example - for 64-bit system we have jdk-6u37-linux-x64.bin.

3. Put this bin file in your home directory.

4. Now need to extract this file to use Java API.

5. Extract BIN file:
  • Make BIN file executable using this command: chmod +x jdk***.bin
  • For executing use: ./jdk***.bin

6. Same named folder will be created in the same directory.

7. Now need to provide JDK path to JAVA_HOME variable in .bashrc file.

8. Edit .bashrc file in editor use following command:

          gedit .bashrc

9. Now put this code in your .bashrc file:

           export JAVA_HOME=/home/jdk***
           export PATH=$JAVA_HOME/bin:$PATH

10. Save file.

11. To refresh your bash settings write this command and hit enter:

            bash

12. To verify configured JDK path use this command:

            which java

This will display your configured path i.e. /home/jdk***/bin/java

Sunday, November 8, 2009

Here lies the roots of java

When the chronicle of computer languages is written, the following will be said: B led to C, C evolved into C++, and C++ set the stage for Java. To understand Java is to understand the reasons that drove its creation, the forces that shaped it, and the legacy that it inherits. Like the successful computer languages that came before, Java is a blend of the best elements of its rich heritage combined with the innovative concepts required by its unique environment. While the remaining chapters of this book describe the practical aspects of Java—including its syntax, libraries, and applications—in this chapter, you will learn how and why Java came about, and what makes it so important.
Although Java has become inseparably linked with the online environment of the Internet,
it is important to remember that Java is first and foremost a programming language.
Computer language innovation and development occurs for two fundamental reasons:

• To adapt to changing environments and uses
• To implement refinements and improvements in the art of programming As you will see, the creation of Java was driven by both elements in nearly equal measure.

Java's evolution

Java is related to C++, which is a direct descendent of C. Much of the character of Java is inherited from these two languages. From C, Java derives its syntax. Many of Java's object-oriented features were influenced by C++. In fact, several of Java's defining characteristics come from—or are responses to—its predecessors. Moreover, the creation of Java was deeply rooted in the process of refinement and adaptation that has been occurring in computer programming languages for the past three decades. For these reasons, this section reviews the sequence of events and forces that led up to Java. As you will see, each innovation in language design was driven by the need to solve a fundamental problem that the preceding languages could not solve. Java is no exception.

The Birth of Modern Programming: C

The C language shook the computer world. Its impact should not be underestimated, because it fundamentally changed the way programming was approached and thought about. The creation of C was a direct result of the need for a structured, efficient, highlevel language that could replace assembly code when creating systems programs. As you probably know, when a computer language is designed, trade-offs are often made, such as the following:

• Ease-of-use versus power

• Safety versus efficiency

• Rigidity versus extensibility

Prior to C, programmers usually had to choose between languages that optimized one set of traits or the other. For example, although FORTRAN could be used to write fairly efficient programs for scientific applications, it was not very good for systems code. And while BASIC was easy to learn, it wasn't very powerful, and its lack of structure made its usefulness questionable for large programs. Assembly language can be used to produce highly efficient programs, but it is not easy to learn or use effectively. Further, debugging assembly code can be quite difficult.

Another compounding problem was that early computer languages such as BASIC, COBOL, and FORTRAN were not designed around structured principles. Instead, they relied upon the GOTO as a primary means of program control. As a result, programs written using these languages tended to produce "spaghetti code"—a mass of tangled jumps and conditional branches that make a program virtually impossible to understand. While languages like Pascal are structured, they were not designed for efficiency, and failed to include certain features necessary to make them applicable to a wide range of programs. (Specifically, given the standard dialects of Pascal available at the time, it was not practical to consider using Pascal for systems-level code.)

So, just prior to the invention of C, no one language had reconciled the conflicting attributes that had dogged earlier efforts. Yet the need for such a language was pressing. By the early 1970s, the computer revolution was beginning to take hold, and the demand for software was rapidly outpacing programmers' ability to produce it. A great deal of effort was being expended in academic circles in an attempt to create a better computer language. But, and perhaps most importantly, a secondary force was beginning to be felt. Computer hardware was finally becoming common enough that a critical mass was being reached. No longer were computers kept behind locked doors. For the first time, programmers were gaining virtually unlimited access to their machines. This allowed the freedom to experiment. It also allowed programmers to begin to create their own tools. On the eve of C's creation, the stage was set for a quantum leap forward in computer languages.

Invented and first implemented by Dennis Ritchie on a DEC PDP-11 running the UNIX operating system, C was the result of a development process that started with an older language called BCPL, developed by Martin Richards. BCPL influenced a language called B, invented by Ken Thompson, which led to the development of C in the 1970s. For many years, the de facto standard for C was the one supplied with the UNIX operating system and described in The C Programming Language by Brian Kernighan and Dennis Ritchie (Prentice-Hall, 1978). C was formally standardized in December
1989, when the American National Standards Institute (ANSI) standard for C was adopted.
The creation of C is considered by many to have marked the beginning of the modern age of computer languages. It successfully synthesized the conflicting attributes that had so troubled earlier languages. The result was a powerful, efficient, structured language that was relatively easy to learn. It also included one other, nearly intangible aspect: it was a programmer's language. Prior to the invention of C, computer languages were generally designed either as academic exercises or by bureaucratic committees. C is different. It was designed, implemented, and developed by real, working programmers, reflecting the way that they approached the job of programming. Its features were honed,
tested, thought about, and rethought by the people who actually used the language. The result was a language that programmers liked to use. Indeed, C quickly attracted many followers who had a near-religious zeal for it. As such, it found wide and rapid acceptance in the programmer community. In short, C is a language designed by and for programmers. As you will see, Java has inherited this legacy.

C++

During the late 1970s and early 1980s, C became the dominant computer programming language, and it is still widely used today. Since C is a successful and useful language, you might ask why a need for something else existed. The answer is complexity. Throughout the history of programming, the increasing complexity of programs has driven the need for better ways to manage that complexity. C++ is a response to that need. To better understand why managing program complexity is fundamental to the creation of C++, consider the following.

Approaches to programming have changed dramatically since the invention of the computer. For example, when computers were first invented, programming was done by manually toggling in the binary machine instructions by use of the front panel. As long as programs were just a few hundred instructions long, this approach worked. As programs grew, assembly language was invented so that a programmer could deal with larger, increasingly complex programs by using symbolic representations of the machine instructions. As programs continued to grow, high-level languages were introduced that
gave the programmer more tools with which to handle complexity.
The first widespread language was, of course, FORTRAN. While FORTRAN was an impressive first step, it is hardly a language that encourages clear and easy-to understand programs. The 1960s gave birth to structured programming. This is the method of programming championed by languages such as C. The use of structured languages enabled programmers to write, for the first time, moderately complex programs fairly easily. However, even with structured programming methods, once a project reaches a certain size, its complexity exceeds what a programmer can manage.
By the early 1980s, many projects were pushing the structured approach past its limits. To solve this problem, a new way to program was invented, called object-oriented programming (OOP). Object-oriented programming is discussed in detail later in this book, but here is a brief definition: OOP is a programming methodology that helps organize complex programs through the use of inheritance, encapsulation, and polymorphism.

In the final analysis, although C is one of the world's great programming languages, there is a limit to its ability to handle complexity. Once a program exceeds somewhere between 25,000 and 100,000 lines of code, it becomes so complex that it is difficult to grasp as a totality. C++ allows this barrier to be broken, and helps the programmer comprehend and manage larger programs.

C++ was invented by Bjarne Stroustrup in 1979, while he was working at Bell Laboratories in Murray Hill, New Jersey. Stroustrup initially called the new language "C with Classes." However, in 1983, the name was changed to C++. C++ extends C by adding object-oriented features. Because C++ is built upon the foundation of C, it includes all of C's features, attributes, and benefits. This is a crucial reason for the success of C++ as a language. The invention of C++ was not an attempt to create a completely new programming language. Instead, it was an enhancement to an already highly successful one. C++ was standardized in November 1997, and an ANSI/ISO standard for C++ is now available.

The Stage for Java

By the end of the 1980s and the early 1990s, object-oriented programming using C++ took hold. Indeed, for a brief moment it seemed as if programmers had finally found the perfect language. Because C++ blended the high efficiency and stylistic elements of C with the object-oriented paradigm, it was a language that could be used to create a wide range of programs. However, just as in the past, forces were brewing that would, once again, drive computer language evolution forward. Within a few years, the World Wide Web and the Internet would reach critical mass. This event would precipitate another revolution in programming.