Showing posts with label files. Show all posts
Showing posts with label files. Show all posts

Saturday, July 23, 2011

Compile and Execute Java Code with packages in CMD

Hello all ;) 


Today I am providing you all quick crash course on how to compile and execute java codes using CMD but the there is a small catch :P the java codes are stored in packages ;)


So, i have created 3 packages A,B,C 
A contains a.java class   [this is the entry point i.e. a.java contains public static void main(String ap[]) method]
B contains b.java class
C contains c.java class


Lets begin, below are the steps accompanied by screenshots to make it easy to understand and implement


Steps are as follows:


1. Create three folders at your desired location the folders are: A,B, and C
    P.S. : I have created these folders on my Desktop :)

    
2. Open your notepad and create a.java below is the screenshot of a.java

    
and save this a.java in A folder
    NOTE: import B.b links the b class stored in B package

3. Open Notepad and create b.java file, below is the screenshot of the code

    
and save this b.java in B folder
    NOTE: import C.c links the c class stored in C package

4. Open Notepad and create c.java file, below is the screenshot of the code

    
and save this c.java in C folder

5. Now comes compilation, We have to be careful in this step. We can not directly compile in any order.
    We have to follow the order, for example in this case where I have A,B, and C package i will compile that java file which is not dependent on any package i.e. which do not import any class which we have created. In this case c class should be compiled first and then b class should be compiled then a class.


If you do not follow this series then you will encounter errors during javac command :(
Below is the screen shot of the cmd during compiling and executing the code

    
NOTE: use / for packages 

6. Give a smile :) you have successfully compiled and executed java files using packages in CMD :) :)

    NOTE: this style can be used in Mac and Linux also :) so enjoy


Thats it for this post


Take Care


Peace


For Any Query mail me: ravjot.singh.28@gmail.com

Tuesday, June 28, 2011

Creating Jar file Using CMD/Terminal

Hello Friends :)


Well i will not provide u the basics of Java coz we all are smart programmers, I will touch areas which are essential for programming but at the same time we don't have sufficient resources to get solution.


My First topic will be to create a jar File


How to create a Jar file? ( very common question )


Well many of us use IDEs (Netbeans or eclipse) to create jar files which is the easiest method.


But there are various tricks in which we have to create jar files manually ( these tricks will be provided in future posts NOT TO WORRY :) ) & for starters this is the best place to learn to create jar files using cmd/terminal 

Steps are as follows

1. Make a folder and in that folder compile your .java files. [let the folder location be C:/../Desktop/x]

2. Now after successful compiling the .java files open your notepad and type in it

Main-Class: <your .java file name which u want to run when you run jar file> <Note:press enter after this line>

[optional] Class-Path: <if you are using any library write them here and separate file name with space> <Note:press enter after this line>


  
Eg:  Suppose my main java file name is “main.java” and i am using three libraries “c.jar lib/a.jar lib/b.jar” . So my notepad will contain

Main-Class: main    <Note:press enter after this line>

Class-Path: lib/a.jar lib/b.jar c.jar <Note:press enter after this line>

Save as the file “main.mf” at the location “C:/../Desktop/x/main.mf”

3.  Now open your cmd/terminal and type in it cd C:/../Desktop/x press enter

4. Now type the command “jar cvfm <your jar file name>.jar main.mf *.class” and press enter

5. Thats it in 4 steps you created your jar file congrats to all :)


Thats it for this post
Enjoy
Keep smiling
Have fun 





For Any Query mail me: ravjot.singh.28@gmail.com