Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Saturday 5 March 2016

Maven : mvn package in Eclipse

Guys, as you maybe aware the command "mvn package" creates a package and stores it in the target directory. On the other side if you run "mvn install" it will create a package and then store it in your local repository.

I assume you have created a project from the archetypes from command line and then converted it to a Maven project after importing it in Eclipse.

Make sure the pom.xml has packaging set to jar by default. You could also change the package to war if needed.

Now simple right click the project to choose Run as -> Run Configurations.


Here you can specify the goals. In this case, we just want to create a simple package. So do this :


As you see, we also get various options for e.g. to build offline, skip tests, debug output etc. Now just enter the goals "clean package" and click Run.

You'll see that after a while a jar package gets created in the target directory of the project. Similarly if you run "clean install", it will install the jar to your local repository location. Make sure to double check the timestamp of the package from Terminal.

Wednesday 2 March 2016

How to open Terminal from within Eclipse on Mac OS X

In Eclipse, from the Help menu -> Eclipse Marketplace, lookup for Terminal. Install the plugin TM Terminal that you'll come across as below:


Once installed, simply mention the shell interpreter location in the Preferences for e.g. /bin/bash and also choose the initial working directory as either your User Home or Eclipse Workspace.


Now go to Window -> Show View and choose Terminal to show it within Eclipse and click the following blue button to open a new session.


This way you can run shell scripts from within Eclipse itself. Cheers!

Sunday 28 February 2016

Recover and restore deleted files from Eclipse Navigator

Have you come across a situation where you accidentally deleted source code from the Eclipse Navigator. Just don't panic in such a case. There's a way to recover and restore these deleted files.

Simply right click the project from the Navigator pane and choose the following option :


In the next screen, you'll see the list of all files that you deleted and wish to restore back. Just choose the necessary files, click Restore and relax!


Thursday 25 February 2016

Ant - Delete only files from directories

For an Ant build, if you just want to delete files without deleting the directory, consider the following. For illustration, we'll take the folder structure as mentioned in our earlier post.

Let's say I want to only clear the class files generated in the bin/com/ironcladzone directory.

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="clean-class-files" basedir=".">

<property name="build.home" value="${basedir}" />
<property file="build.properties"/>

<target name="clean-class-files">
<delete>
<fileset dir="${bin.dir}">
<include name="**/*"/>
</fileset>
</delete>
</target>


</project>

Note : by using **/*, it will recursively delete all the files from the bin/ subdirectories. Using the <fileset> tag, we mention the directory tree, on which the delete operation needs to be performed.

Wednesday 24 February 2016

Build properties in Ant - An Introduction

Guys, in our previous post we saw a basic build.xml file with some basic target definitions. Note that we mentioned a property or two in it. But have you ever wondered, what happens if you have multiple properties to define? Yea that's when we use a build.properties file.

Let's create a build.properties file and we define all the properties inside it. The advantage of using a properties file is that whenever you want to modify/update the value of any property you only have to do it once in this build.properties. You don't have to look into the build.xml to see when and where you want to modify things. This makes life a lot easier, which otherwise would have been cumbersome.

Ok so I create a new build.properties with the following updated folder structure :


You must have observed that in our older post we included some hardcoded directory paths directly in the build.xml. That's a quite crude way to define the paths and is not advisable. Hardcoded paths should be avoided as much as possible. So what we do is create some properties and define their values equal to the directory paths. Something like this :

versionnum=1.3
workspace.src=/Users/ironcladzone/Documents/workspace/AntBuildTest/src
src.dir=/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src
bin.dir=/Users/ironcladzone/Documents/workspace/AntBuildTest/build/bin

Note that we have also included an additional property named "versionnum" to assign a certain version number to the jar package that we created.

Now we only need to provide a reference to these properties in our build.xml file. Take a look at the new updated build.xml below :

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="create-jar" basedir=".">

<property name="build.home" value="${basedir}" />
<property file="build.properties"/>

<target name="clean">
<delete dir="${build.home}/src" />
<delete dir="${build.home}/bin" />
<delete dir="${build.home}/lib" />
<delete dir="${build.home}/test" />
<delete dir="${build.home}/dist" />
</target>

<target name="createdir" depends="clean">
<mkdir dir="${build.home}/src" />
<mkdir dir="${build.home}/bin" />
<mkdir dir="${build.home}/lib" />
<mkdir dir="${build.home}/test" />
<mkdir dir="${build.home}/dist" />
</target>

<path id="classpath">
<pathelement location="${build.home}/bin"></pathelement>
</path>

<target name="copydir" depends="createdir">
<copydir src="${workspace.src}"
dest="${src.dir}"></copydir>
</target>

<target name="compile" depends="copydir, clean">
<javac classpathref="classpath" includeantruntime="false"
srcdir="${src.dir}"
destdir="${bin.dir}"
includes="**/*.java"></javac>
</target>

<target name="create-jar" depends="compile">
<jar basedir="${build.home}/bin" destfile="${build.home}/dist/TestJar-${versionnum}.jar"
includes="**/*.class"></jar>
</target>

</project>

Don't you think the new build.xml looks much cleaner now? It does! Right? Okay so with today's post I've introduced you to the concept of properties. In the next post, we'll try to refine the script and introduce some new features into it. Keep visiting folks.

BONUS TIP : Note that properties are immutable. So once a build starts, the value assigned to the properties remain constant. However, if am not wrong, we can update the properties at build time using the propertyFile task. Note that this is different than the <property file> tag. Notice there is no space there in the propertyFile task.

Ok guys, signing out for the day. Let's meet up again tomorrow. Till then, ciao!

Friday 19 February 2016

Arithmetic Operators in Java - Live practical examples : Part 1

In today's post we'll cover the topic of arithmetic operators in Java with some real live examples. In this post, we'll skip the theory and go straightaway for some practical examples. With these examples, you will get to grasp and understand the operator functions much quicker than ever.

Check out the following code in which we have used multiple combinations of arithmetic operators in different positions.

package com.ironcladzone;

public class operator1 {

public static void main(String[] args) {
System.out.println("Example 1 :" + 3 + 4 + 5);
System.out.println("Example 2 : " + 3 + 4 * 5);
System.out.println("Example 3 : " + 3 + 12 * 10);
System.out.println("Example 4 : " + 3 * 4 + 5);
System.out.println("Example 5 : " + 3 * 4 / 5);
System.out.println("Example 6 : " + 5 / 4 * 5);
System.out.println("Example 7 : " + 10 / 4 + 5);
System.out.println("Example 8 : " + 10 + 4 / 5);
System.out.println("Example 9 : " + 10 + 15 / 5);
System.out.println("Example 10 : " + 125 / 10 / 5);
System.out.println("Example 11 : " + 50 / 10 / 5);
System.out.println("Example 12 : " + 23 / 10 / 5);
System.out.println("Example 13 : " + 23 / 10 / 5 + 4);
System.out.println("Example 14 : " + 3 + 4 + 5 / 7);
System.out.println("Example 15 : " + 3 + 4 + 5 * 2);
System.out.println("Example 16 : " + 3 + 5 / 5 * 2);
System.out.println("Example 17 : " + 3 + 22 / 5 * 2);
System.out.println("Example 18 : " + 3 + 7 / 5 * 2);
System.out.println("Example 19 : " + 2 + 8 / 5 * 2);
System.out.println("Example 20 : " + 3 * 9 / 5 + 2.59);

}


}

Output : 

Example 1 :345
Example 2 : 320
Example 3 : 3120
Example 4 : 125
Example 5 : 2
Example 6 : 5
Example 7 : 25
Example 8 : 100
Example 9 : 103
Example 10 : 2
Example 11 : 1
Example 12 : 0
Example 13 : 04
Example 14 : 340
Example 15 : 3410
Example 16 : 32
Example 17 : 38
Example 18 : 32
Example 19 : 22
Example 20 : 52.59

If you observe closely, you may find a few of the combinations are tricky. For instance, note that the + sign we have used, is for string concatenation and not to be confused with the plus sign used for addition.

This was just the first set of live examples. Another exhaustive post on more examples is on its way. Stay tuned folks. Am also quite sure this code will help you cover the basic operator concepts if you're planning to give the Oracle Java Certification 1Z0-803. Good luck guys. Signing out.

Sunday 14 February 2016

Print File size using Java

If you ever want to print the file size using Java, use the following piece of code below :

import java.io.*;

public class FileSize {

public static void main(String[] args) {
File f = new File("//Users//ironcladzone//Downloads//image1.jpg");
System.out.println("File size in bytes = " +f.length());
System.out.println("\nFile size in kb = " +f.length()/1024);

}


}

Output :

File size in bytes = 241833

File size in kb = 236

Note that when we use the length() function, it gives us the file size in bytes. For getting output in kilobytes, divide it by 1024. If you want size in megabytes, again divide it by another 1024. So on and so forth...

Saturday 13 February 2016

Create a new directory using Java

In today's example we'll create a new directory using Java. Check out the following code below :

import java.io.*;

public class Create_Directory {

public static void main(String[] args) {
File dir = new File("//Users//ironcladzone//Downloads//TestDir");
boolean DirectoryCreated = dir.mkdir();
if (DirectoryCreated)
System.out.println("Created a new directory");
else
System.out.println("Directory not created");
}


}

Note that we have used the function mkdir() as expected. It returns the value "true" if the directory is created or "false" if not created. We use the variable 'DirectoryCreated' to store this boolean value, based on which it prints a message.

Try executing this program twice. The first time when you execute the program, a directory gets created and you get the output like :

Created a new directory

The second time when you execute the code, a directory does not get created since it already exists. So you'll get the output like :

Directory not created

Accept User Input in Java

For accepting user input in Java refer the following piece of code below :

import java.io.*;

public class User_Input {

public static void main(String[] args) {
String inp="";
System.out.println("Please enter your name : ");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
inp=br.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
System.out.println("Welcome " +inp);

}


}

Output:
Please enter your name : 
IronCladWriter
Welcome IronCladWriter

Make sure to import java.io.* so as to use the input/output functions. Notice that we have wrapped an InputStreamReader within a BufferedReader. Guys, do check out detailed documentation on BufferedReader and InputStreamReader for better understanding.

Friday 12 February 2016

Basic string manipulation in Java with Real Examples : Part 1

Hi guys, how you doing today? I just noticed I haven't written anything about Java in quite some time now. Well let's bring Java back into the spotlight with this light subject on basic string manipulation.

Lets try to cover some simple examples covering different scenarios :

Illustration 1 : Extract a substring from a string

In this example we simply extract a part of the string.

public class String_manip {

public static void main(String[] args) {
 String str = "Hello guys. Welcome to IroncladZone";
 
 String sub_str = str.substring(23, 35);
 
 System.out.println (sub_str);
}

}

Output : IroncladZone

Illustration 2 : Replace a substring from a string

In this example we replace the word "guys" with "goodfellas" within the string.

public class String_manip {

public static void main(String[] args) {
String str = "Hello guys. Welcome to IroncladZone";
String replaced_str = str.replace("guys", "goodfellas");
System.out.println(replaced_str);

}

}

Output : Hello goodfellas. Welcome to IroncladZone

Note that strings are immutable. Their value cannot be changed once its created. However we can create a new string with the replaced word/part.

Illustration 3 : Join / Combine two strings i.e Concatenation

In this we simply join 3 strings together using +

public class String_manip {

public static void main(String[] args) {
String str1 = "Welcome";
String str2 = "to";
String str3 = "IroncladZone";
System.out.println(str1 + str2 + str3);
System.out.println(str1 + " " + str2 + " " + str3);

}

}

Output : 
WelcometoIroncladZone
Welcome to IroncladZone

Illustration 4 : Print the length of the string


public class String_manip {

public static void main(String[] args) {
String str = "Welcome to IroncladZone";
System.out.println(str.length());

}

}

Output :
23

Note the length() function used to show the length of the string.

Illustration 5 : Split a string on a whitespace

public class String_manip {

public static void main(String[] args) {
String str = "Welcome to IroncladZone";
String[] new_str = str.split("\\s+");
System.out.println(new_str[0]);

}


}

Output :
Welcome

Notice the \\s+ regex for the whitespace.

That's it for today fellas. I'll try to include some more advanced scenarios in a 2nd part of this post. There's more to come guys, stay tuned...

Thursday 11 February 2016

Create Runnable Jar in Eclipse

Here's a quick pictorial on how to create an executable jar in Eclipse. Assuming you have a Java project in place, let's take a quick look into the steps.

Go to File menu -> Export. Expand Java to choose Runnable JAR File and click Next. Check out the screenshot for reference.


In the next screen, you simply have to choose the Destination folder where you want to save the executable Jar. Also choose the java application's Launch configuration. It also lets you save an Ant script describing the Jar file that you created. Check out this link for official detailed reference.

Thursday 20 March 2014

Show hidden .classpath .project files in Eclipse

If you're using Eclipse IDE for any Java based projects, you might want to make .classpath or .project files visible.

In order to do so, click the small triangle on the right top side of Project Explorer and select Customize View.

Just deselect / un-tick the .* resources option and refresh your project.

Now you will be able to see the .classpath & .project files, which are hidden by default. Note that the .classpath file will contain information about the classpath and build info while the .project file will contain the project metadata.

This is useful while working with Maven. When you use the command mvn eclipse:eclipse, it creates .classpath & .project files, which are not visible in the project explorer by default. You can make them visible in Eclipse Project Explorer like this.


Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker