Unable to find the keystore created by a jar file - java

I have created a program that creates a keystore. When I run this code on IDE, it runs perfectly. However, when I run the jar file - I get no errors but cannot find the keystore created.
Some additional info - The jar file has been created in the cmd prompt using the following commands:
javac -XDignore.symbol.file Keygenerate.java
echo Main-Class: Keygenerate >manifest.txt
jar cvfm myjar.jar manifest.txt *.class
The reason i use -XDignore.symbol.file option is because I am using some deprecated methods in my java file. My file runs perfectly fine on Netbeans but does not clean and build. This is the reason I am using command line to compile and create the jar.

You may need to add the manifest file to the META-INF folder of myjar.jar
Can you show the project and jar folder structures?

Related

Build jar depending on a lib like org.json without ide

I build a project without any IDE and want to compile it. I dont want to use Maven or Ant. So i have several classes and a jar file (org.json) "json.jar".
javac -cp json.jar *.java
java -cp .;json.jar App
Now i want to create another jar file for my project but dont know how to include/link the lib json.jar. I created a manifest.mf
Main-Class: App
Class-Path: json.jar
and tried
jar -cmf manifest.mf App.jar *.class json.jar
that creates a jar file but i cant run it. Thank you
according to jar man page :
Be sure that any pre-existing manifest file that you use ends with a
new line. The last line of a manifest file will not be parsed if it
doesn't end with a new line character.

Add a jar file to another jar file

In my project I have this code that tell class loader to load Driver.class like so:
Class.forName(org.gjt.mm.mysql.Driver);
In Eclipse it runs with no problems and I have created the Jar file of the project. But I don't know how to insert the
mysql-connector-java-5.1.7-bin.jar
into a Jar file of my project. The folder structure look like this:
MANIFEST file:
Manifest-Version: 1.0
Main-Class: server.MultiServer
I am assuming that you finally just want to run your code as
java -jar myjar.jar
There are two options.
Keep mysql-connector-java-5.1.7-bin.jar next to your jar in the same folder and add classpath: mysql-connector-java-5.1.7-bin.jar to the manifest.
Copy all the classes in mysql-connector-java-5.1.7-bin.jar to your jar. Do not copy the jar but the classes in the jar. This is called a fat jar or uber jar. You can automate the same using maven shade plugin.
When you invoke your application jar add the -cp or -classpath option and provide the path to the dependent libraries, here the mysql-connector-java-5.1.7-bin.jar.
for example refer the below example
java -jar -classpath C:\myproject\lib\mysql-connector-java-5.1.7-bin.jar myproject.jar

How to run my a Java program as an executable .jar file?

I was wondering how can I turn my program (which I am currently running from Eclipse) into an executable file?
This will make it more feasible as people with JRE can just access it by clicking on a single file.
Any help would be appreciated.
Thank you.
Compile your java code
Create a manifest.txt file.
Specifiy the main class in the manifest file like: Main-Class: com.example.Main
Create a jar by adding the manifest.txt and required .class files: jar cvfm myJar.jar manifest.txt *.class
Note: make sure to add .class files, not .java files.
You may create this as a maven project and then simply run mvn clean package and the jar will be created in target directory.

Javapackager: Cannot find or load main class

I have created a JavaFX project and am able to run it with the command
java -classpath [very-long-list-of-class-paths] danIDE.Main
However, when I try to use javapackager to create a jar out of it and run with java -jar out.jar, the prompt says Error: Could not find or load main class danIDE.Main
The command I used to create the jar is
javapackager -createjar -v -classpath [very-long-list-of-class-paths] -srcdir src -outfile out -appclass danIDE.Main
I have googled for a long time on this problem and I still couldn't find the solution. Can someone point me to the right direction? Thanks a lot!
Edit:
Here is the project structure.
and here is the exploded jar.
New exploded jar as #Garry requested:
Since you're using IntelliJ IDEA I suggest you let IDEA create your JAR file for you.
First, open up the module settings window:
Then, add a new artifact:
Select JAR From modules with dependencies:
Select your main class in the window and decide if you want to repackage all classes from your dependency JARs in your JAR (extract to target JAR option) or if you want to distribute them alongside your JAR (copy to the output directory and link via manifest option):
If you want to build it whenever you build the project (probably a good idea), click the checkbox for that:
When you next Make the project, the JAR will show up under out/artifacts:
If you didn't click the checkbox for building the JAR when you build the project you can build the JAR from the Build Artifacts option in the Build menu.
Can you try using below command? Make sure to update 'classes' folder to the Base directory of the files to package.
As you said in question that you are able to run danIDE.Main so I assume all the required classes are available in dist folder.
So create a folder out in the project parallel to dist
javapackager -createjar -classpath [very-long-list-of-class-paths] -appclass danIDE.Main -srcdir dist -outdir out -outfile out.jar -v
Updated: as per the uploaded screenshot: point -srcdir to dist, now the generated jar out.jar will be placed in out/out.jar

Maven cannot create a jar file

I used for building and creating a jar file this command as l know
$ mvn -Pzinc package
and that also
$ mvn -f m2-pom.xml package
and it built successfully !
but i can't find a jar file ?
In your project's target directory you'll able to see the generated jar file.

Categories

Resources