Include a jar archive without including in environment variables - java

I want to include org.eclipse.swt.3-1.jar in a .java file without put it in my environment variables? How can I do this?
The java src I want to try is this.
I have just this file as a .java file and I have from here http://www.java2s.com/Code/Jar/o/Downloadorgeclipseswt31jar.htm the org.eclipse file. Both in the same directory.
PS: I compile on the command line and don't use Eclipse.

its just 2 files, the .jar and a .java - both in the same directory
Then you need to first compile the java source (.java) file into a class file (.class) file. You use the javac command from the JDK and something like
javac -cp org.eclipse.swt.3-1.jar;. file.java
Then your directory should contain 3 files, te .jar and .java and a .class file. Then
java -cp org.eclipse.swt.3-1.jar;. file

Related

Create .class file from a jar file -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

Is it possible to combinate .Java with .Jar to make one .Jar?

Actually I have :
Modify.java
commons-io-2.4.jar
And when I compile I must everytime specify that I use commons-io-2.4.jar like :
javac -cp .;lib\commons-io-2.4.jar Modify.java
Is there a way that I run my program like this :
java MyProgram.jar
Compile you .java file and get the .class file.
add .class file in correct package folder inside .jar file.
It should work.
Or unjar the existing jar. Add new .class file to correct folder hierachy.
Re-jar using
jar cf jar-file input-file(s)
Make sure you use the correct archive format [java archive]

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

javac does not compile the Java code even though the jars are present and available

I am trying to compile a Java source file on command prompt with the following command
C:\temp\test>javac -cp ".\*;" *.java
but the class does not get compiles, I have errors of type files not found, or could not find resource.
Even though the jars are present in the same directory as the Java files.
C:\temp\test>javac -cp ".;*.jar" *.java
See http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html
It is easy to forget that a .jar file is a compressed file system structure, not a .class file. When you specify a directory in -classpath, all .class files in that directory are available to the loader. Specifying a .jar file is analogous to specifying a directory, but in addition to the .class files in the .jar file's root directory, it also makes available .class files in the packages compressed into the .jar structure. Remember that the package structure mirrors a directory structure.

Categories

Resources