Export JAR file in Eclipse

In this tutorial, I will show you how to export your eclipse project into jar file.

Open Eclipse and the project you would like to make into JAR.

Right click on the project folder in Eclipse -> Export, just as it is shown in the figure below.

Figure 1.1. Export option in Eclipse file expolorer

After clicking on Export there will be displayed prompt, which looks similar to Figure 1.2.

Figure 1.2. Ecplise Export prompt

On this prompt select JAR file. This will take you to the next prompt (Figure 1.3).

Figure 1.3. Export JAR File Specification

This is important step. In the part, where is marked with (1) you select the src folder and the files, which you want to include in the jar file. In (2) select them.

In (3) mark “Export generated class files and resourses”, this option will include compiled class files in the jar file.

The next step (4) is to select the export destination. That means where you want to be located the jar file. You can export it in desktop.

In (5) mark “Compress the contents of the JAR file”, which will compress the content and the jar file size will not be that huge. After this options, click NEXT.

Figure 1.4. Jar Specification prompt.

The last export option prompt is for the Jar Specifications.  Here I have marked you the field, which you should modify. You should pick the class, which contains main() method. This is important step, if you want to make your jar runnable. After that you just press Finish and your jar file will be made in the destination folder.

Compile multiple Java classes in Command Prompt

I. Introduction.
In this article it will be shown how to compile multiple Java classes in console (Command prompt) or Terminal.

As a prerequisite you should have Java downloaded, installed and set the JAVA_HOME environment variable.

II. Compiling multiple source files in console.

The example project, which will be used contains two files:

  • MessageDeliver.java – Example file, which contains one private field and method
  • AppMain.java -> the main class, which calls method from MessageDeliver

MessageDeliver.java

public class MessageDeliver {
	private String msg = "Hello!";

	public String getMsg() {
		return this.msg;
	}
}

AppMain.java

public class AppMain {
	public static void main(String[] args) {
		MessageDeliver mDeliver = new MessageDeliver();

		System.out.println(mDeliver.getMsg());
	}
}

In order to build both files, open command prompt or terminal and type:

javac MessageDeliver.java AppMain.java

javac command calls java compiler. If you have configured correctly JAVA_HOME variable then in the folder, where are located MessageDeliver.java and AppMain.java should appeared two new files MessageDeliver.class and AppMain.class.

III. Running Java files from Command Prompt.

Running files after being compiled in Command Prompt is done by writing the following command:

java AppMain

IV. Conclusion.
In this article, It was shown how to compile multiple files and to run them in Command Prompt. The examples were done in Windows OS, but the process is similar in Linux terminal.

Thank you for reading this article! If you have any questions – feel free to ask in comments section.

Reflection API feature in Java – Overview

I. Introduction.

This is the first post from series of articles with main subject – Java reflection.
In this article, I am going to explain the basic concepts of reflection – what is it, when it is being used, why and show examples for its usage.

II. What is Reflection in terms of Java?

Reflection is powerful feature implemented in Java and it allows to the executing Java program to examine or to modify internal properties at runtime.

The packages, which are required in order to use the features, which Reflections API provides are in reflect, you can import them using following:

import java.lang.reflect.*;

Reflection gives an information about the class to which one object belongs to. In addition to that – the methods of the class that can be executed with the usage of the object.

III. Example – print methods of a given class.
Let see the feature in practice.

The demo contains following files:

  • Cat.java
  • CatRflDemo.java

in class Cat, we have following:

Cat.java


public String name;
private int years;
private boolean isHungry;

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

public String getName() {
    return this.name;
}

public void sayMEOW() {
    System.out.println("MEOW!");
}

In order to get and to print the methods, which are in Cat class, we use snipper similar to this bellow:

Method[] catMethods = Cat.class.getMethods();
for(Method mthd : catMethods) {
    System.out.println(mthd.getName() + "()");
} 

This will print all of the methods of class CAT.

After executing this, the following result will be printed in console:

getName()
setName()
wait()
wait()
wait()
equals()
toString()
hashCode()
getClass()
notify()
notifyAll()

As you may notice, there are methods, which are extended and aren’t written by us like toString().

IV. Example 2 – set value to a field.
Another great feature, which is part of Reflection API is the option to set value to a field.

I will use Cat class for this example. In the cat class, I added some fields. Here is it:

public String name;
	
private int years;
private boolean isHungry;
	
public void setName(String name) {
	this.name = name;
}
	
public String getName() {
	return this.name;
}
	
private void sayMEOW() {
       System.out.println("MEOW!");
}
	
public boolean isTheCatHungry() {
       return this.isHungry;
}

As you see there is new method called isTheCatHungry. In the main class put this:

Cat catObj = new Cat();

System.out.println("Is the cat hungry?" + catObj.isTheCatHungry());

Field flds[] = catObj.getClass().getDeclaredFields();

for(Field fld : flds) {
     if(fld.getName().equals("isHungry")) {
          fld.setAccessible(true);

  try {
        fld.set(catObj, true);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

   }
}

System.out.println("Is the cat hungry?" + catObj.isTheCatHungry());

Here we create object of Cat called catObj and print the default value of the variable isHungry. After that, we need an array to store all of the declared fields, so this is what the purpose of flds array is. Followed by iteration through the array.

The line: if(fld.getName().equals(“isHungry”)) {}
checks the field, which is going to be accessed, which in this case is called “isHungry” and after this it is being set to accessible, so we could control it and on the next step it is being modified with the following method fld.set(catObj, true);

After running this snippet, the output will be something like this:

Is the cat hungry?false
Is the cat hungry?true


V. Conclusion.
Java Reflection API provides great variety of features. In this article some of them are being shown. Each feature provided by Reflection API has its advantages, but when using it, the dev should warn. Just as Oracle wrote it – if operation could be made without using reflection, it is preferable to avoid it. However, Java’s reflection API is really usefull and could help dev to learn more in depth how the stuff works inside VM.

In the next article from this series I will cover more details, explore more features of Reflection API, and give more in depth examples.

Thank you for reading this article! If you have any questions – feel free to comment.