How to update a file inside a jar directory [duplicate] - java

This question already has answers here:
How can I replace a class file in jar files?
(4 answers)
Closed 7 years ago.
I have seen similar answers but none of them helped me so far. I want to know if there is any command to update a class file inside a Jar directory.
I have this Jar:
META-INF/
META-INF/MANIFEST.MF
src/
src/project/
....
src/project/dir/Myfile.class
META-INF/dir/
etc..
I have modified Myfile.class and I want to update the existing inside the jar's directory.
I tried
jar uf myJar.jar -C src/project/dir Myfile.class
but it does not work as expected. The jar is still the same.
Any ideas?
Edit: Working on cmd

jar uf myJar.jar -C src/project/dir Myfile.class
will place Myfile.class in the root of the jar. Instead, do
jar uf myJar.jar src/project/dir/Myfile.class

Related

how to create an executable jar file from an folder [duplicate]

This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 6 years ago.
I have taken an executable jar file from server to my local system and then I have unrar it through win rar and then I have made some changes in the .class file now i again want to make executable jar file of the folder that get created when I initially unrar it please advise how can i create executable jar file again
folks please advise with context to creating an executable jar file
Let's say that all your project file's are in folder named MyApp
then
open command prompt
navigate to directory where MyApp is placed using cd
execute command jar cvf MyApp.jar MyApp
EDIT
(To make this jar executable:)
create a file named MANIFEST.MF and put it in META-INF folder
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.somepackage.MyApp
OR you can simply use Launch4j to wrap up your program into an executable: http://launch4j.sourceforge.net/

How to run java eclipse project from command line? [duplicate]

This question already has answers here:
How to run a Java project in command line
(6 answers)
Closed 7 years ago.
I'm using a mac, and I'm trying to run a java project created in Eclipse from the command line. My project folder has a bin and src folder, each of which have folders game and display corresponding to packages. There are .java files in src/*/ and .class files in bin/*/
Here's what worked:
I compiled everything using javac src/*/*.java. I ran my project by going to the src folder (cd src) and using java game.Gomoku following the directions in this answer. Everything worked. The only problem was that now I had a bunch of .class files in my src folder, and I wanted them in my bin folder like Eclipse organized it.
The problem:
I figured out that I could do this with javac -d bin src/*/*.java. But then when I try java bin/game/Gomoku or java game/Gomoku or java -cp bin bin/game/Gomoku I get the error Error: Could not find or load main class bin.game.Gomoku. How can I run my project?
So while I was typing my question I came across a solution. After going to bin (cd bin) I could run my project with java game.Gomoku. I also found that my question had been previously answered here, but I'll post this anyways just in case my method for compiling (in my question) can help anyone.

Executable JAR file also contains the source code

I know this question asked many times but I can't find my answer from older question .I have four packages an I want to create jar file from this packages my main program is in main package . I use this statement like so :
jar -cvfm app.jar manifest.txt *
When I saw the content of a jar file my sources (.java) also exist in the jar file but I want just to have .class file in my jar file .How can I solve this problem?
You may change the command to:
jar -cvfm app.jar manifest.txt *.class
You missed the .class part. By using the wildcard * you've included everything.
Good Luck.

Specify Java CLASSPATH variable [duplicate]

This question already has answers here:
Specifying classpath for a jar
(5 answers)
Closed 8 years ago.
I am not familiar with Java and I am trying to run a piece of Java code packaged as jar file.
On Windows command line, I specify the CLASSPATH to my jar folder like this:
set CLASSPATH="D:\jarFolder"
And there's a test.jar file in that folder. But when I run this
java -jar test.jar
it still failed with this error:
Unable to access jarfile test.jar
I can run the test.jar by specify the full path. But I want to know why the CLASSPATH doesn't work. My understanding is, it tells the java runtime where to locate the jar file.
Jarfiles have to be on the classpath explicitly. Specifying a classpath dir for a jarfile is not sufficient.
See the related Oracle docs on the Java command line:
When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored.
Try to use
java -jar D:\jarFolder\test.jar
If you know main class name of your entry point of jar. You can use
set CLASSPATH="D:\jarFolder"
java yourpackage.yourEntryPoint
You can learn your entry point of your jar file using a zip program to extract jar file and reading file entry point section of manifest.mf file.
When you use -jar option, you need to specify which jar. Think of it this way. If you do not give jar name, which jar java should execute? You may have 10,20 may be 100 jars in your class path.
But if you give class name, then java searches among jars and execute correct one.

How to build generated and executable jar file [duplicate]

This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 9 years ago.
I have a directory in which I generated *.class files. How can I build executable jar file from this files.
What I need to know is
how to clean dir
how to put all files in jar and make it executable
Sorry people i didnt mentioned, i need do this programmatically in java code.
java cfe myJar.jar Classname.class myClass
and for exceution
java -jar Jarname.jar
Creating a Jar
If you're using an IDE such as Eclipse or Netbeans, there are specific easier ways to do it. If not I think the following command would work:
jar cfe <jar-file> <main-class-name> <class-files...>

Categories

Resources