Tuesday, December 23, 2008

Storing java command output to text file.

Its quite simple to store Java command output of the console window of MS Windows i.e command prompt to store in a text file. The need for this is when we have huge text as an output on executing a java program, sometimes we face problem in searching specific text.
The command is:
commandprompt>java GenerateLog >> UserFile.txt

here in the above example, java GenerateLog is the command to execute GenerateLog java file, and output will be dislayed on the console. But by providing the ">> UserFile.txt" at the end of it , the output will be directed to the file called UserFile.txt

By using the above command, a text file named UserFile will be stored in most probably c drive or in the current location
You can easily find this file.

Comment will be highly appreciated...

How do I configure User tools in editplus to compile and run java file

I would like to demonstrate how easy it is to configure java environment in editplus.

1.Open your installed editplus editor
2.Go to Menu -> Tools -> Configure User Tools
3. A Preferrences titled window will be opened on your desktop.
4.Find Add Tools button under the section Groups and tools item: section on the right side of the window.
5. Click the button Add Tools -> Program
6. It will create an item as New Program in the List.
7. Below the list you will find few input boxes such as menu, command, argument etc.
8. Fille those with following details
Menu Text: CompileJava
(Note: Here i am creating a user tool which will compile the java program written in edit plus without going to the command prompt and compiling manually)
Command: %JavaInstalledDir%\jdk1.5.0_14\bin\javac.exe
(Note: %JavaInstalledDir% means the location where you have installed Java. for example c:\jdk1..5\bin\javac.exe, so write c:\jdk1..5\bin\javac.exe in command inout box.Dont copy paste %Installed......... content. Please verify wether javac.exe file is available in the given location)
Arguments: $(FileDir)\$(FileName)
(Note: place the above content as it is)
9. Select the capture output window check box.
10.Click Apply and Ok.
11. A CompileJava item will be added under the menu of editplus i.e. Menu -> Tools -> CompileJava
12. Now its very simple to compile the Java file. Open editplus , create a simple java file having no user defined import packages, save it to whichever location you want.Keep the file opened on the editor and go to the itemCompileJava which you have created in step 11. Click it.
It will create a small partition in lower half of editplus and will show the message as
Output Completed.. .. Normal Termination.
13. A class file will be created in the same location where you have placed the file.

How to Configure User Tool for executing the Java Class file in Edit plus.
14. Follow the steps from 2 to 7.
15. The input parameters as
Menu Text: ExecuteJava
Command: %JavaInstalledDir%\jdk1.5.0_14\bin\java.exe
(Note: Follow the note in the above sections, written for Arguments input box value.)
Arguments: -classpath $(FileDir) $(FileNameNoExt)
(Copy paste as it is)
16. Follow steps 9-10.
17. Verify the new tool item as ExecuteJava in your edit plus editor
17. Use steps 12-13 to create a java class file using editplus editor , and the click ExecuteJava item in Menu -> Tools > ExecuteJava , created by you to run/execute it.
You can see the output in the lower half of the editplus editor.
I hope, i am able to help you in setting up Java environment in Edit Plus.

For your convenience, i have added the image of the user configuration tool settings of Edit Plus below:



Note: View the image by increasing the view size of your browser
Your comments will be higly appreciated..

First Java Program Example

Hello All,

The Java prorgam looks like as follows:

HelloWorld.java
-----------------
public class HelloWorld
{
public static void main(String [] args)
{
System.out.println(" Hi. to all");
}
}
--------------------
Things to note:
1. Java file name must be same as the class name (in above example its HelloWorld) which is defined as public and having main method. This is in case of a stand alone program where you are trying to execute the first program in Java.
2. The Parameter passed in main method i.e. String [] args, is actually the referring to the arguments provided while calling the main method through the console command.

How to Compile & Run.
------------

1. Create a folder such as c:\JavaWorks
2. Save the above file withing the newly create folder with the name as HelloWorld.java
3. go to your start menu, click on run
4. enter text cmd and press enter. It will open up a command prompt.
5. go to the folder location in the console i.e your command prompt should be pointing to c:\JavaWorks
6. Verify Java Development Kit is already installed in your machine. for more details please refer to my earlier article on how to install java on my machine.
7. After installation verification, enter javac HelloWorld.java command console and press enter.
8. By pressing enter , a class file with the name HelloWorld.class will be created in the same folder, where you have placed the Java file.This class file is the compiled code which will be actually executed.
9.Verify, if any error message shown on the prompt, if compiled successfully it will show the prompt again without any message.
10. Now to execute the class file which you have generated in step 8, enter java HelloWorld on your command prompt , where you have compiled you java file, and press enter key.
11. It will show you a message as "Hi. to all" .
12. For your help, i am copy pasting the commands on the console here in sequence.:

==========================
C:\JavaWork>javac HelloWorld.java

C:\JavaWork>java HelloWorld
Hi. to all

C:\JavaWork>
==========================

If you have face any issue, drop a comment here.