How to compile a java project to obtain a .jar file [duplicate] - java

This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 2 years ago.
hello someone could suggest me how to compile the java project that I attach to generate the sri.jar file that appears in the Readme file
//Project java to compile
https://github.com/joselo/sri
//command line where the sri.jar that I want to generate appears
java -jar sri.jar /path/sample/certificate.p12 cErTiFicAtEPaSsWoRd /path/sample/unsignedFile.xml /path/sample outputFile.xml

To create a jar file first thing that needs to be done is to compile the project using javac
javac -cp ./sri-master/lib/*: ./sri-master/src/sri/*.java -d ./out/
and then run the below to create the jar
jar cvfm xyz.jar ./sri-master/manifest.mf -C ./out/ .

Related

compile and run java with jar files [duplicate]

This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 1 year ago.
I'm trying to compile a java file that uses multiple jar files as imports.
the command that I have used to compile my code :
javac -cp jackson-databind-2.12.1.jar:jackson-core-2.12.1.jar:jackson-annotations-2.12.1.jar TestRunner.java
as a result two .class files are created : TestRunner.class and TestRunner$1.class
then I run the command :
java TestRunner
but it throws an error that says:
Error: Unable to initialize main class TestRunner Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/type/TypeReference
I have included all the required libraries in the javac command, tested it with the IDE and it works fine.
I have tried other versions of the jackson library but I'm stuck with the same error.
You need to specify the classpath when running your code, by using the same -cp args that you used when compiling, plus the folder where your compiled class is in.
In your cas that would mean something like java -cp .:jackson-databind-2.12.1.jar:jackson-core-2.12.1.jar:jackson-annotations-2.12.1.jar TestRunner
The libs you specified are not included in the .class files generated, so Java still needs them to understand how to call the code that's not coming from your class file.

When does manifest file created in java? [duplicate]

This question already has answers here:
Use of the MANIFEST.MF file in Java
(2 answers)
Closed 6 years ago.
I have a basic question about the manifest file: when is this file created?
Is it created in the .class file when we compile a java file? Or should we create the manifest file from the command line after the .class file is created?
Update: you can find the answer here: Use of the MANIFEST.MF file in Java
The manifest file has nothing to do with the compilation/running process of a class.
If you configured your project correctly in your IDE (depending on the IDE), it will be generated during the build project process.
If you are working using the command prompt and notepad, you can always create it manually.

can't access jar files from terminal compilation [duplicate]

This question already has answers here:
Package not found; javac
(2 answers)
Including jars in classpath on commandline (javac or apt)
(5 answers)
Closed 8 years ago.
I am compiling java files from terminal. But It gives error for external jar files. where can I put external jars. so, it can be used at compile time from terminal?
You can pput it in every directiory and then add it to the classpath:
javac -cp <path_to_jar_including_filename> <class_to_compile>

how to compile a jar file in source code [duplicate]

This question already has answers here:
How do I create a ZIP file in Java?
(2 answers)
Closed 9 years ago.
Is it possible to compile class file to jar file not using jar -cvf options.
I want to compile jar files by source code.
Cant somebody show me some example codes
A JAR simply is a ZIP file.
While I am not quite what you are trying to achieve, I can give you these hints:
Compiling: javac Hello.java
Creating a JAR: zip Hello.jar Hello.class
If you want to have a JAR containing your sources, you could as well run the above command with: zip Hello.jar Hello.java
Also note that, if you are using a build tool like for example maven, there are various plugins for suchs tasks such as 'assembly' (for maven).

How to make executable program in java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert my Java program to an .exe file?
How to make executable program in java?
You don't really need to, there are a number of other options available:
As Philip suggested you can use an executable jar file. See http://www.ibm.com/developerworks/library/j-jar/index.html
You can build on top of a framework that already has its own launcher such as Eclipse. See http://wiki.eclipse.org/index.php/Rich_Client_Platform
You can use a technology such as Java web start to launch your app from a browser. See http://en.wikipedia.org/wiki/Java_Web_Start (or just package your app as an applet)
probably this may help you
http://www.javacoffeebreak.com/faq/faq0042.html
there he talks about nice tool called Java2Exe which serves your purpose
The common way is the following:
compile your class files: javac *.class
create a jar file (it's basically a .zip-file):
echo Main-Class: MyMainClassName > manifest.txt
jar cvfm MyOwnJarfile.jar manifest.txt *.class
create a .bat-script (or sh script if your are on unix):
echo java -jar MyOwnJarfile.jar > start.bat
Double-Click on start.bat ;-)

Categories