简体   繁体   中英

How to make a jar file from code?

I have some java code written in a text file, now I want to create a jar file to use that file as a library in my android project. So what would be the best way to make a jar file from this point. Please note that I have some code written in a textfile and saved as a .txt. If I rename that file to .java and use this code

jar cf filename.jar file(s)

a jar file is created but when I decompile it in java decompiler it doesnt show the packages and that codes that's why I am unable to use it's methods. What would be the best way to do this? I need help

Creating a jar File in Windows Command Prompt

Start Command Prompt. Navigate to the folder that holds your class files:

cd \mywork

Rem -- Set path to include JDK’s bin.  For example:
path c:\Program Files\Java\jdk1.5.0_09\bin;%path%

Rem -- Compile your class(es):
javac *.java

Rem -- Create a manifest file:
echo Main-Class: DanceStudio >manifest.txt

Rem -- Create a jar file:
jar cvfm DanceStudio.jar manifest.txt *.class

Rem -- Test your jar file by trying to execute it
Rem -- This will not apply for a library JAR file without any main
java -jar DanceStudio.jar

If you want to use the classes in your library they must be compiled and you have to manually create all the packages subfolders.

So : 1. Make a folder 2. Create each package subfolderds in it (com/foo/bar/xxx) 3. Compile each of your .java files and put them it the correct subfolders 4. Zip your folder and rename it with a .jar extension

But it is hard to do this by code, why don't create it with your IDE ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM