Create .class file from a jar file -java - java

I am using Java8. I have one .jar file containing .class files.
I downloaded the source code of the same .jar file. And I edited one of the file in it. It is .java file.
Now I want to compile all these .java files in the source code and create a new .jar file with .class files
Any clues?

There are many options
1. if you want to do it from command prompt then you would need to set the classpath and then either create a list of java files with package name and use it for compiling, something like this
# Linux
$ find -name "*.java" > source.txt
$ javac -classpath "${CLASSPATH}" #source.txt
:: Windows
> dir /s /B *.java > source.txt
> javac -classpath %{CLASSPATH}% #source.txt
or use build tool like Ant.
Once you have the .class files, you can use "jar" command to create a jar of the .class files
2. Use IDE - you can use any of these IDEs - eclipe, intellij, netbeans.
You need to setup a project with the java files and compile project and export as jar using class files.
I think it would work out better for you to use an IDE.

1) Compile the classes (javac command)
If you don't have any jar dependencies required at compile time, you should not need to set the classpath.
Otherwise you should specify the cp argument with.
Example (Unix way) :
javac -cp "myLib.jar:myOtherLib.jar" *.java
2) Then create a jar from these compiled class (jar command)
Example :
jar cf myjar myFolderWithCompiledClass

extract your jar to a folder and run this:
javac [folder]/*.java
read that for more info:
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html

Related

Combining .java files and exporting them as an executable jar from java code

I want to know how to export a jar file from within java code. Look at the following situation:
I have some .java files including one main class. I want to know how to pack those files and convert them to an executable jar file. I don't want to do this the traditional way by exporting the project to a jar file using eclipse built in functionality (I know how to do that). I want to know how to export the .java files and the required libraries to a jar file using java code.
Here is my ideal result: I run my own jar exporting program and it asks me to select some files. When I do select and hit 'export', it packs those files in an executable jar file. Thanks in advance and peace out.
Maybe you could try running cmd commands for creating JAR file using JAVA code.
You can create jar File in Command Prompt following these steps:
- Start Command Prompt.
- Navigate to the folder that holds your class files:
C:\>cd \mywork
- Set path to include JDK’s bin. For example:
C:\mywork> path c:\Program Files\Java\jdk1.7.0_25\bin;%path%
- Compile your class(es):
C:\mywork> javac *.java
- Create a manifest file
manifest.txt with, for example, this content:
Manifest-Version: 1.0
Created-By: 1.8.0_171 (Oracle Corporation) #your jdk version
Main-Class: mainPackage.MainClass
- Create the jar file:
C:\mywork> jar cvfm Craps.jar manifest.txt *.class
To see how can we run these commands within JAVA code, follow this link:
Run cmd commands through Java

how to use JDatePicker jar file into the program

I need to use 'JDatePicker' class for a program. I downloaded 'jdatepicker-1.3.2.jar' file. and when i extracted the file using the command "jar xf jdatepicker-1.3.2.jar", I got 3 folders in the directory " META-INF, tutorial, net". But i don't know how to import this to the java program and where to store this jar file and the extracted folders. could any one assist me on the above?
I don't use any IDE. I run the java programs in command prompt
You must have that exact jar file on the class path. When compiling specify jar in the compile
javac -classpath jars\jdatepicker-1.3.2.jar -sourcepath src\project -d classes src\project\MyProg.java

Need help building a Selenium Java Class file from cmd line

I'm trying to cmd line compile a .java selenium test script into a class file that I can run from the command line.
All of my selenium jar files and all other supporting jar and lib files are in C:\JarFiles
My CLASSPATH is set to C:\WDJarFiles*
I am working at the command line here: C:\EclipseIDEworkspace\MC3\src\Tasks
My .class files are located here C:\EclipseIDEworkspace\MC3\bin\Tasks and I'd like to be able to update them at that location.
My folder structure was set up by using Eclipse IDE so I'd like to keep the existing folder structure but now I want to be able to compile my .java files from the command line and update the .class files.
So, when I run javac like this:
javac Edit.java
It compiles OK and the .class file gets created in the same folder where I am running the javac command -- but -- I also get a huge number of other .class files in this same directory! These look like supporting class files.
I'm not sure what my cmd line javac syntax should be to:
Compile my .java file so its .class file gets updated in the C:\EclipseIDEworkspace\MC3\bin\Tasks folder.
I don't get all those other .class files created in my working folder C:\EclipseIDEworkspace\MC3\src\Tasks
Thanks for any help...
You should try the '-d' option to specify an output directory:
javac Edit.java -d ..\..\bin\Tasks
About the multiple other .class files, you probably have many nested classes into your Edit.java file?

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
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 ?
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

compiling a packaged project dependent on a jar

This seems to me to be a trivial question, but I've had a lot of trouble getting an answer.
I've developed a project in eclipse that is dependent on a jar file, which resides in the project's root directory. All my files are in a package "a.b.c" in a src folder. It runs just fine in eclipse. I now want to run this project from the command line. I do this command to compile the project:
javac -classpath dependency.jar -d ./bin/ ./src/a/b/c/*.java
Everything is compiled into class files and put into the bin/a/b/c folder. Then I do these commands to run the project:
cd bin
java -cp ../dependency.jar a.b.c.Main
Now I get "java.lang.NoClassDefFoundError: a/b/c/Main".
So, how do I run a project that is in a package and depends on a jar file?
Just include the current dir in the classpath as well - i.e. java -cp ../dependency.jar:. a.b.c.Main
You also need to specify your compiled files on the classpath, these will contain your a.b.c.Main. On *nix flavor machines the path separator for cp is the colon (:), and on windows it's a semicolon (;), so on *nix, your run command should be (because you're running from the bin directory):
java -cp ../dependency.jar:. a.b.c.Main

Categories

Resources