Saturday, November 14, 2015

Converting java code to executable jar file through command prompt

Assume we have set of java files written using an IDE. To conver the file as executable jar file through command prompt and run it across machines. Here are the steps to follow:

§  Create a new folder . Eg: java2jar

§  Create another folder in the newly created folder and copy all the .class files that are required for the program to execute.
Eg: java2jar> src> copy all the files here.

§  Create a folder in the main folder and name it as lib or anything of your choice and copy all the dependable jar files. This is optional and if you have dependable jars.
Eg: java2jar>>lib>>copy all jar files here.

§  Open command prompt in Windows click on windows icon , type ctrl + R and then enter cmd .

§  Navigate to the path where you have created the new folder that has all .class files. 
>>cd C:\Users\lenovo\Desktop\java2jar

§  Type path and enter the path for jre available in the machine.In my machine it is available in the following location.
>>cd C:\Users\lenovo\Desktop\java2jar>path  C:\Program Files\Java\jre1.8.0_45\bin

§  Create new text document in java2jar(my folder name) and rename it as manifest.txt file.

§  Open manifest.txt and type as follows:

Class-Path: lib\jar1.jar lib\jar2.jar lib\jar3.jar #continue  entering all jar files available 
Main-Class:src\main.class                              #Enter the main class file that has main() method.

If you don't have dependable jars then skip Class-Path and enter only Main-Class.

§  Save and close the manifest.txt file.

§  Navigate to command prompt and type the command
>>cd C:\Users\lenovo\Desktop\java2jar>jar cvfm myjar.jar manifest.txt src\*.class

Explaning the above command:
myjar.jar is name of the jar file that you want to create
manifest.txt is the manifest file that has headers
src\*.class adds all the .class files available in the src folder.


§  Navigate to command prompt and type the command
>>cd C:\Users\lenovo\Desktop\java2jar>jar xvf  myjar.jar 

§  It creates META-INF folder in java2jar and it has manifest.MF.

§  Now from the command prompt type 
>>cd C:\Users\lenovo\Desktop\java2jar>java -jar myjar.jar

It runs the program from the command prompt.



Creating a batch file:

Open a new text document in java2jar folder and name it as run.bat

Enter java -jar myjar.jar in the file and save it.

Now double click on run.bat .

The complete folder java2jar can be shared across any machine.
















String contains in python

s = "A nice string search 234"
res = ""
items = ['a', '4', '6']
for x in items:
#print x
if s.find(x) == -1:
res = x + " is not in given string"
print  res
else:
res = x + " is in given string"
print res