I have an executable .jar-File which works, but I want to add some files to the .jar-File with another program. My idea was to use the 7zip command line extension, but when I try to add something to the .jar-File. I use this command:
7za.exe a -t7z C:\myfiles\thejar.jar C:\filestoadd\* -r
Everytime I try to do this the CMD throws me an error and says:
Error: C:\myfiles\thejar.jar is not supported archive
Well, ok. Then my Idea was to unpack the file thejar.jar, add the files to the directory where the files from thejar.jar were extracted and create a .zip with the extension .jar. When I did this, the file thejar.jar was about 1MB smaller than before with adding files to it. I tried different compression-methods but it was always smaller. When I tried to execute the .jar, an error-message popped up and said Invalid or corrupt jarfile. I already googled my problem, but I haven't got an answer for now... Can you help me?
The simple / recommended solution is to use the jar command that is included in every Java JDK to add the extra files to the JAR.
It is also possible to create a JAR file using JarOutputStream:
How to use JarOutputStream to create a JAR file?
The trouble with using 7zip, or any other "standard" zip utility is that you might accidentally use some modern zipfile feature that the Java utilities don't understand.
You also asked (in comments) if you are permitted to copy the jar.exe and jli.dll from an Oracle JDK / JRE into your own project.
Read the license! We are not lawyers here and we can't give you proper legal advice.
My understanding is that that would be a violation of the Oracle license and copyright. Embedding a full JRE is permitted, but cherry-picking is not permitted.
Consider using OpenJDK instead. The licensing for OpenJDK is GPL based and there are no copyright-based restrictions on redistributing a vanilla distribution. Not even modifying and redistributing it ... so long as you make available source code and build instructions for any changes you make to it1.
Finally, note that this is moot in Java 11 onwards. Oracle now only distributes full JDK's. So that means that if your customer installs "Java" from Oracle, the jar command will be included.
1 - You only have to worry about trademark. Basically, the trademark license says you are not permitted to call a product "Java", etc if it deviates from the "standard" in non-permitted ways. Lookup the details for yourself.
-t7z will create a 7z archive, not a zip. You need -tzip instead.
I know this is an old post but if anyone is searching for info the following works great and makes a jar that will execute correctly, I ran across this post myself looking for info and in the end came up with the following.
For me this method was the easiest way to do what I needed, and just easier and faster then using jar, plus works great as batch file
C:\Progra~1\7-Zip\7z.exe a -tzip -mx9 -spf -r Filename.jar *
Related
Can anyone please post detailed, step-by-step instructions how to install Saxon (10 HE) and how to run a transformation from the command line in MacOS (10.13.6)?
I have installed Java on my computer.
I have downloaded the SaxonHE10-6J.zip file from SourceForge.
Based on the recommendation here I have placed the saxon-he-10.6.jar in the myUserName/Library/Java/Extensions folder.
I then opened the Terminal application and entered a command based on this answer:
java -jar saxon-he-10.6.jar -'/Users/myUserName/Documents/path/to/mystylesheet.xsl' -s:'/Users/myUserName/Documents/path/to/some.xml'
This results in:
Unable to access jarfile saxon-he-10.6.jar
All my attempts, including moving the .jar file to the /Library/Java/Extensions directory or including a full path to the .jar file within the command failed with the same error message.
Please note that I am not a Java developer and I do not intend to use this in an application. All I want is to be able to perform an occasional transformation.
I should also note that the locations of my XML and XSLT files may change from one case to another. I would like to be able to keep the .jar file in a constant location and supply the paths to the XML and XSLT files as required - IOW, I want to have a command template where I only need to change the 2 filepaths (and possibly add some options to the transformation).
What do I need to do?
Eventually I got it working. Although these are not exactly the detailed, step-by-step instructions I was hoping for, I will summarize here what I have learned so far. Hopefully this will save someone the hours of frustration I had to go through.
Make sure you have Java installed on your computer. If not, download
from Oracle and install.
Download Saxon from SourceForge. Unzip and place it anywhere on your
hard disk, except:
do not place it in /Library/Java/Extensions or in myUserName/Library/Java/Extensions;
do not place it within a folder whose name contains a / (see below for description).
To initiate a transformation, make your command:
java -jar 'path/to/saxon-he-10.6.jar' -xsl:'path/to/mystylesheet.xsl' -s:'path/to/some.xml'
Alternatively, you can use:
java -cp 'path/to/saxon-he-10.6.jar' net.sf.saxon.Transform -xsl:'path/to/mystylesheet.xsl' -s:'path/to/some.xml'
This form can be also used to run XQuery by changing
net.sf.saxon.Transform to net.sf.saxon.Query.
For adding more options and/or parameters to your command, see the
instructions given here:
https://www.saxonica.com/html/documentation10/using-xsl/commandline/
but do not follow the instructions at the top of the page regarding
the form of the basic command.
Corrections/additions are most welcome.
Description of the problem with folder name containing /:
Put all 3 files (saxon-he-10.6.jar, mystylesheet.xsl and
some.xml in a folder named XML/RSS in my Documents folder;
Ran the following command:
java -jar '/Users/myUserName/Documents/XML:RSS/saxon-he-10.6.jar' -xsl:'/Users/myUserName/Documents/XML:RSS/mystylesheet.xsl' -s:'/Users/myUserName/Documents/XML:RSS/some.xml'
Received the following error:
Error: Could not find or load main class net.sf.saxon.Transform Caused by: java.lang.ClassNotFoundException: net.sf.saxon.Transform
Moved only the .jar file to the parent folder and ran the following
command:
java -jar '/Users/myUserName/Documents/saxon-he-10.6.jar' -xsl:'/Users/myUserName/Documents/XML:RSS/mystylesheet.xsl' -s:'/Users/myUserName/Documents/XML:RSS/some.xml'
Result: successful transformation.
Moved the .jar file back, renamed the folder to XMLRSS and ran the
following command:
java -jar '/Users/myUserName/Documents/XMLRSS/saxon-he-10.6.jar' -xsl:'/Users/myUserName/Documents/XMLRSS/mystylesheet.xsl' -s:'/Users/myUserName/Documents/XMLRSS/some.xml'
Result: successful transformation.
I don't ever use it myself: however myUserName/Library/Java/Extensions is special as far as the classpath is concerned (you don't need to put JAR files in this directory on the classpath), but it's not special as far as the -jar option is concerned - that needs to be an absolute or relative filename in the normal way and has nothing to do with the classpath.
If you've chosen to put the JAR file in this magic location, then I would use the command java net.sf.saxon.Transform options to pick Saxon up from the classpath rather than identifying the -jar location directly.
There are good reasons for NOT putting Saxon in this magic location, however; one reason is it will affect applications that don't actually want to use Saxon (they might be written to use some other XSLT processor, and you might not actually be aware that they use XSLT at all, until they stop working).
I'm a rookie as to java.
I'm using an open source library named JCIFS.In its ZIP package,it has patches.And I want to use some functiones that only the patch has.But I don't know how.There are lots of irrelevant messages while I use google.Thus I ask here.
Can anyone help me?
Thanks in advance.
In ZIP package you probably have sources (maybe in src directory). I mean lots of *.java files and some with other extension. You have to apply patch on this files. At the beginning of patch it is specified which file you need. And then compile it all using javac and maybe package into jar.
If you don't have sources you have to get source for given class.
Then you have to apply patch on this file and compile it using javac. Then you will get .class file. You have to replace this file in .jar or .zip archive used with your system.
On Unix, you have the standard patch program to do that, but that ofcourse isn't normally present on Windows. If you want the patch command (and lots of other Unix utilities) on Windows, you could download and install Cygwin.
But looking at the patch file, it's very small and you could easily do the change by hand. Look at the patch file: The lines with a - in the left column must be removed. The lines with a + must be added.
In the Java source from http://download.java.net/jdk6/source/ I get a jar of size ~130mb. The jar don't attach to Eclipse, and inside it has a file called "X_X" of size ~130mb.
Does anyone know what's happening?
--update
Thanks Gerco Drie,
now, about this README file:
JDK requires a high level of
technical expertise. (...) If you are not a
technical professional in one of these
categories, this release is probably
not for you.
so, where is the already buid one for me?
I'm using Java 6 update 15.
--update
The site http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/jdkfiles.html says the src.zip is in the root jdk folder (yes, I remember it was last time I needed it), but its not..
Is my jdk a pirate version? :P
You need to run the jarfile, not extract it or link it to eclipse. The instructions are here:
http://download.java.net/jdk6/6u20/promoted/b01/docs/build/README-JRL.html
These files are jar files that need to be run, not un-jar'd, for example: java -jar filename.jar
I think BalusC is right by the way, this is not the sourcecode you want. You probably want the rt.jar sourcecode, which is in src.zip with the JDK. What you downloaded is the complete sourcecode of the JVM, hotspot compiler, etc included. Although it also includes the class libraries, there is an easier way.
The distribution you download is a self-extracting file which displays a license YOU MUST ACCEPT before the things you need are extracted.
Run it, and see if it answers your issues.
This question already has answers here:
How can I get the latest JRE / JDK as a zip file rather than EXE or MSI installer? [closed]
(30 answers)
Closed 8 years ago.
I have seen many products bundeled with JDK, I wonder if there is a way where one can install JDK by simply unzipping contents to a directory, so there is no icon created in add/remove programs, no registry entries etc.
Also in this case:
How can we configure Java plugin for browsers?
And how can we configure settings as seen via Control Panel entry for Java?
According to this, I created a batch script to build jdk archives automatically.
The essential parts of the link are:
Create working JDK directory ("C:\JDK" in this case)
Download latest version of JDK from oracle (for example "jdk-7u7-windows-i586.exe")
Download and install 7-zip (or download 7-zip portable version if you are not administrator)
With 7-zip extract all the files from "jdk-[6-7]u?-windows-i586.exe" in directory "C:\JDK"
In command shell (cmd.exe) do the following:
change directory to directory C:\JDK.rsrc\JAVA_CAB10
execute command: extrac32 111
Unpack C:\JDK.rsrc\JAVA_CAB10\tools.zip with 7-zip
In command shell (cmd.exe) do the following:
change directory to C:\JDK.rsrc\JAVA_CAB10\tools\
execute command: for /r %x in (*.pack) do .\bin\unpack200 -r "%x" "%~dx%~px%~nx.jar" (this will convert all pack files into jar)
Copy whole directory and all subdir of c:\JDK.rsrc\JAVA_CAB10\tools" where you want your JDK to be and setup manually JAVA_HOME and PATH to point to your JDK dir and its BIN subdir.
Yes, you can create a zipped up JDK, unzip it on the target machine and run java, javac, etc. from that directory to your heart's content.
The easiest way to create such a zip file is to install the JDK on a machine and then zip up the contents of the JDK directory. We do this in certain circumstances where we need to control exactly what configuration of Java will be used in our deployments. In that case, our scripts just point JAVA_HOME (or the equivalent) to our internally bundled JDK instead of relying on a sysadmin to install exactly what we need prior to arrival.
In terms of integrating with the browsers, that can be a bit more problematic. The short answer is no, you can't integrate directly with the browser without some sort of installer.
You could use SysInternals RegMon and FileMon (now owned and dist by MS) to see exactly what is modified by the Java installer. I believe there will be a number of reg entries that you will want to create. Products like WISE installer, for example, also use this sort of approach under the hood to repackage product installations (e.g. as MSI).
Please be careful since there are also some dynamic decisions made by the installer which may affect what gets installed (e.g. on XP v.s. W2K3 server). I was bitten by this once regarding installed codepages. I do not recall the precise details, but the effect was that a codepage file was missing in my embedded JRE + JDK (legally redistributable portion only). This caused a very bizarre and seemingly nonsensical runtime error in my code. It goes without saying that the same applies to Server v.s. client JVM DLLs.
Really, no, at least if you want to use it from Windows and not from, say, cygwin. Windows depends too much on the registry; you could simulate the registry updates necessary, but software that moves the files to the right place and updates the registry is called "an installer"; you'd just be reinventing the installer.
I believe this at least used to be feasible to a limited extent with earlier versions of Java - I don't know if it still is.
Most of the registry entries are used for things like browser plugins, as you mentioned in the question. If you just want to be able to run Java (e.g. from a batch file), that's one thing - but really installing it is a different matter. Even if you're just wanting to run it, you'll need to be careful to always explicitly use the one you intend, rather than using the installed one accidentally for part of your application.
What's your actual use case? Do you actually need a browser plugin? What aspect of the configuration are you interested in? A lot of the control panel configuration is to do with updates and browser integration. Many other aspects can be controlled using command line options to set specific system properties.
If you just want to provide a JVM with your application is fine, but more than that I would not recommend.
If you just want to have the JDK (JRE) files, you can run the installer within sandboxie. Once installed in the sandbox, just copy the files from c:\sandbox and you are done.
I'm using this to compile and run legacy java applications which cannot be migrated easily to a newer version of java. I can point Eclispe to this JDKs and tell it to be compliant to Java 1.3.
Just down load the Windows server version of Java from the Oracle downloads page. Setup JAVA_HOME and PATH variables on your own.
I have an exe that I know was written in java. I understand that java programs can be made into an exe and there are tools to convert jar files to exe but is it possible to convert back? AFAIK jar files can be run on any platform that can run java and I would like to use a windows compiled java program on mac without using any extra virtualisation (wine,vmware)
It depends how the exe has been built:
If it has simply wrapped, with a tool like JSmooth, the same tool can extract the jar
If it it has been compiled, with for instance gcj (as illustrated by this question),... I am not sure.
If it has been compiled by a static compiler (or AOT - Ahead-Of-Time -), I believe it is not possible to extract the orignial jars.
If your application was wrapped using JSmooth, you can look in your default temp directory (C:\Documents and Settings\Username\Local Settings\Temp) while the application is running.
Open a windows explorer window to the temp dir, then start up your application. You should see a jar file show up (Temp#.jar). Just make a copy of this and you should be on your way.
i suggest just rename your file extension from .exe to .jar and try to extract them.
to view the code use java Decompiler LIKE:
http://java-decompiler.github.io
It is possible to convert file.exe back to file.jar , To go with this reversing process to will require you to know how the file was beeen encrypted ,for example if is the first level encryption class name and files are not hidden , for the second level file classes are hidden , for the 3rd level file and classes are hidden . to get this well download jar to exe software and try how they convert jar to exe then you will be able to know the reverse. to know how you proceed with reversing the process there is a single blog which helped me and all tools you may use are listed there like : Resource Hacker
Winhex
Ollydbg 1.10+ MemoryDump 0.9 and Olly Advanced or StrongOD Plugin(for advanced ctrl+g).
DJ Java Decompiler
7-Zip or Winrar
The link:
https://reverseengineeringtips.blogspot.com/2014/12/unpacking-jar2exe-21-extracting-jar.html?showComment=1480364662658#c447064983483780468
thanks