<$BlogRSDUrl$>

Java @ Command-line with a Text editor

How to write java programs using a text editor and from command-line...

Monday, December 29, 2003

Using arg files to with javac
-----------------------------------
If I have multiple files to be compiled and multiple command-line options that i do not want to retype everytime I want to compile, I can store them in files and invoke javac as follows:

javac @option_file @arg_file

Rules for creating these files are as follows:
-------------------------------------------------------
. Contents can be blank-space seperated or new-line seperated
. the paths in the file are relative to the current directory
. * is not allowed

Creating the files:
----------------------
option_file is a file that contains the command-line options to be supplied to javac

Contents of my options.bat:
-d c:\MyClasses
-classpath innerClasses
arg_file is the file that contains the names of files to be compiled.
follow the above-mentioned rules to create the file

Contents of my arg_file.bat:

innerClasses\InnerClassDemo.java

In the above case I want to compile the class InnerClassDemo.java in the directory
innerClasses in the current directory

Executing javac
--------------------
At command-line, go to the direcotry where your arg_file.bat and options.bat are stored.

C:\MyExp>javac @ options.bat @arg_file.bat
posted by NotSure  # 10:07 AM
|

Sunday, December 28, 2003

Compiling multiple .java files in a package
----------------------------------------------------
If you have multiple .java files in lets say c:\MySrc\multipleFiles,
you can compile all of them as

C:\>javac MySrc\multipleFiles\*.java
posted by NotSure  # 9:59 AM
|
Setting the classpath at command line
------------------------------------------------

To eliminate the need to constantly define/use the -classpath switch, I created
a .bat file (myClasspath.bat) and saved in in C:\.

At the begining of a development session I call the .bat file from command-line.
C:\> myClasspath.bat

This eliminates repeatedly having to use the -classpath option with javac.

The following are the contents of myClasspath.bat

set CLASSPATH=%CLASSPATH%;C:\MyClasses;C:\myExp

posted by NotSure  # 9:41 AM
|
Compiling the java files into another directory.
----------------------------------------------------------

Suppose my source(.java) file/s (in this case our old friend HelloWorld.java)
is/are in c:\MySrc and I want to put my .class files in C:\MyClasses,
I can do it as follows using the -d option while compiling:

c:MySrc\> javac -d c:\MyClasses HelloWorld.java
posted by NotSure  # 9:13 AM
|

Friday, December 26, 2003

In my recent foray into command-line java programming,
I have hit a road block. How to compile and run a simple
java program? Eureka, I have finally found the solution
and am presenting my findings to my co-aspirants:

I am using TextPad as my code-editor.

Background:
I have my HelloWorld.java in C:\myExp

Step1:Make sure that java is properly installed
-----------------------------------------------------------
From command line type java -version
If alls well, you should get info about your JRE version and type of VM running.

Step 2: Compiling a java program from command line
-------------------------------------------------------------------
C:\>cd myExp
C:\myExp>javac HelloWorld.java

Step 3:Running the program
------------------------------------
C:\myExp>java -cp C:\myExp\ HelloWorld

This runs your program. Thats all.. We're done here...finito..

Happy Coding

posted by NotSure  # 3:07 PM
|

Tuesday, October 28, 2003

Setting the Java classpath and path variables in Windows XP

[1] Intro and background about PATH and CLASSPATH:
----------------------------------------------
All your java programs need both these variables to be properly set in
order to run.

PATH: This defines the location of the various executable(.exe) programs eg. java, javac
This should point to the bin directory of your jdk installation(henceforth referred to as
YOUR_JDK_HOME). That is YOUR_JDK_HOME\bin

CLASSPATH: this variable defines the places on the file system that java can look for to
locate the various classes required to run your application. They can be a semi-colon seperated
list if you ahve your programs stored at various places.

You have to set it to YOUR_JDK_HOME\lib
In addition to the above, you can append all the directories in which class files required for you application
will be found. Each entry is to be seperated by semi-colons.

eg: YOUR_JDK_HOME\lib;C:\myPrograms;C:\otherJars

[2] Now how do we set it???
-----------------------
Go to Start Menu-->Control Panel-->System-->Advanced-->Environment Variables-->

Under System variables click the new button, a popup comes up, fill out the following values:
variable name: CLASSPATH
variable value: YOUR_JDK_HOME\lib;OTHER_PLACES_ YOU_PUT_YOUR_FILES

choose the path from the system variables list and click on edit.
to the end of the variable value append
PRESET_PATH_VALUES;YOUR_JDK_HOME\bin

click OK

[3] Restart the computer.
-------------------------

[4] Verifying your settings.
------------------------
1. Open a command-line window.
2. Type echo %path%
3. You should see the value that you set.
4. Repeat the above for classpath (ie: echo %CLASSPATH%)

[5] EXTRA INFO
--------------
If you get the java.lang.NoClassDefFoundError, then your PATH and CLASSPATH variabales are NOT properly set.

You are all set to go. Happy coding!


posted by NotSure  # 9:12 AM
|

Archives

This page is powered by Blogger. Isn't yours?

Weblog Commenting and Trackback by HaloScan.com