how to create .jar file? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

how to create .jar file?

I have a java code ready, but I don't know to invoke without using IDE. help pls.

22nd Feb 2017, 3:46 PM
Bala Krishnan Swaminathan Mani
Bala Krishnan Swaminathan Mani - avatar
5 Answers
+ 6
To execute your Java code there is no need to generate a jar file from it. You can compile them using the javac executable and then you can execute your code using java. You have to set classpath (where compiled classes are) and the class to be executed (it must contain the main method). For example: java -classpath c:\mycode\classes mypackage.Main On Linux you have to set classpath in different way than on Windows. But to answer the question, you can create a jar file this way: jar cvf myjar.jar *.class If you want to be able to execute jar via: java -jar myjar.jar then you have to add a manifest to the jar which describes which class should be executed. See documentation for details.
22nd Feb 2017, 4:24 PM
Tamás Barta
Tamás Barta - avatar
+ 4
It was java, not javac I guess. You set classpath wrong or you really don't have class. We should see it to help. What is your class name? Do you use a package? Do you have compiled class file next to the java file? Where they are located on your disk? What is the command you executed? What platform do you use? Does your class contains valid main method? There are a lot of chance to fail :)
22nd Feb 2017, 4:36 PM
Tamás Barta
Tamás Barta - avatar
+ 1
I tried executing my java program using javac executable, but getting an error "Error: Could not find or load main class first"
22nd Feb 2017, 4:32 PM
Bala Krishnan Swaminathan Mani
Bala Krishnan Swaminathan Mani - avatar
+ 1
My classpath was misspelled, it works now. Thanks a lot.
22nd Feb 2017, 5:11 PM
Bala Krishnan Swaminathan Mani
Bala Krishnan Swaminathan Mani - avatar
- 1
yeah,
29th Mar 2017, 9:00 PM
Nohsevin Fortiva
Nohsevin Fortiva - avatar