I have a Java application that was converted to an .exe with launch4j. For several reasons I need to get access to the class files of the application.
The first thing I tried was unpacking the exe with 7zip. That way I get a handful of class files, but definately not the whole application packed in the exe (the class files seem to belong to launch4j).
What other options are there?
You can use something like this to extract the JAR from the Launch4j executable:
http://www.nirsoft.net/utils/resources_extract.html
Typically the executable packages necessary JAR's as executable resources and then looks for a JDK on the path to execute.
Once you get the JAR file you should be able to decompile it.
You cannot get access to class files. Exes are created, So that nobody can get access to your code.
If somehow , you are able to get access to jar files then you can decompile them to get the code and make changes.
Related
I have a huge JAR file, which I created with Maven Shade plugin.
I run this with java -jar foo.jar , and my UI opens. Now I want to execute *.exe file, which is also in that JAR file, how can I do this?
I tried putting the exe to my classpath and run it from there, but after trying I found out that classpath is actually the location, where my JAR is.
Any suggestions?
Found this thing here, but is this really the best solution? Seems like alot of work, I think I have different case here, because I can define the location of exe myself and the JAR is created by me.
run exe which is packaged inside jar
Why I need this?
I want to give user a single file executable, which he can run, but my program is using the *.exe. Should I put the exe next to my jar and there will be 2 files or there is solution for my requirements?
Copying the file to a temporary location and running it is the way to go. The answer you linked to does much more work that necessary, as you can get your exe file as an InputStream and copy it to a file with a utility like Apache Commons IO FileUtils.copy(in, out)
See How do I copy a text file from a jar into a file outside of the jar? for example.
It's not about the location, it's about the fact that you need to tell your OS to run the exe and, unfortunately, you can't do that by providing a location within a jar.
I store some objects in files and everything works fine in Netbeans console. But when i run the JAR through the command line (java -jar myapp.jar) i get this:
Where should i place the files in order to be able run the app through the command line?
The answer will depend on if you want to write to the files or not...
You could...
Ensure that the files are placed relative to the Jar file and use a relative path. The problem with this is if the execution context is not the same directory as where the jar and files are stored, you won't be able to find them again...
This will also be dependent on your build process to make sure that any required files are placed copied to the build location of the Jar
You could...
Place the files within a well know location, for example, on Windows you could use {user.home}/AppData/Local/{application name} or on Mac you could use {user.home}/Library/Application Support/{application name} and place the files here, then you could use an absolute path to the files
This likely becomes an installation issue, as you need to ensure that any required files are copied to the required location when the application is installed.
If the files are auto generated at runtime, then you just need to make sure the directories exists and make them if they don't
You could...
Store the files within the Jar context (AKA embedded resources), the means by which you do this will depend on your IDE and build process, for example, in Netbeans, you can copy files into the src directory of your project and they will automatically be included in the resulting Jar file. I believe that Eclipse can work the same way. However, if you're using Maven, you will need to place the files into the resources directory instead.
You would then access these resources using Class#getResource or Class#getResourceAsStream depending on your needs.
This will make the resources read-only however.
I am developing a GUI using swing that runs an executable. Currently the executable is being used via Runtime.getRuntime().exec().
I have both the executable and the source code. If I compile my GUI into a jar the executable will not be included into as it currently stands, correct?
I would like the whole thing to run as a single file, is it better then to use the source code or can I package it all as one jar when I'm done some how?
Though I'm writing all the code by hand I do have WindowBuilder for eclipse, I haven't really explored it thoroughly, is there anything in there that might help?
EDIT: Sorry, to clarify: The GUI I want to build uses an executable called src2srcml to take a source file (C, C++, Java) and convert it to an XML File. src2srcml is a separate executable I got from this website: http://www.sdml.info/projects/srcml/
I want to embed this executable into my GUI so that when I compile my GUI into a JAR it contains src2srcml inside it so that I don't need to send a client both my GUI and src2srcml separately.
If you include the executable within the Jar, the executable will not be runnable by the OS.
With that in mind, you have a number of choices...
You Could...
Design a build process that compiles and packages the jar file, takes that and the executable and copies it into an appropriate directory structure for distribution...possible even building installers...
This will mean that you can still access the executable at runtime by using a relative path
This does mean you will have at least two files you will need to distribute, but the over all process is relatively painless...
You Could...
Include the executable within the jar file. At runtime, you would need to extract the executable to the filesystem and execute it.
This means that you will have at least one file you will need to distribute, but it complicates the development process as you will need to find the resources (executable) at run time and extract it to some location before you can execute it
You Could...
Just build an installer for your application. Something like like izPack for example, or what ever installer you want to use.
This means that you can distribute a single file to the client, but when installed, it will unpack all the files into the installation location and you won't need to care...
I'm not sure if I understood your question, but if you want to export you application into jar file and you want it to include all dependecies just have a look at this SO question: How to create a jar with external libraries included in Eclipse?
I had running executable jar of a Java project. Unfortunately I have lost all the source code but I still got the executable jar of it. Is there any possibility to extract my classes from the jar?
I have tried extracting but class files are in damaged shape.
To get source from .class files , just download a decompiler from here and get them.
Are you talking about decompiling or just receiving the class files? The class files can be extracted using any software capable of reading ZIP. If you talk about decompiling that usually will work purly. But you could try Jad which I had the best experience with.
If there's one thing that annoys me about Java it's that you can't double-click a class file so as to run. I assuming there's an entry in the registry that has to be edited to do this but I haven't a clue.
So, as it says on the tin. Does anyone know how to associate Java class files to run on double-click on Windows (I aiming for Windows 7 here but I'm sure there'd be no difference in the three most latest releases)? It would make my life (and I'm sure many other people's) much easier!
Udpate: I've seen answers relating to making a JAR out of the class in question and running it that way. However useful, that is not exactly what I'm looking for here. I'm effectively looking for Windows itself to invoke java with the class on double-click, with the correct arguments.
if classpath doesnt matter too much, easily done with a simple batch file runjava.bat or so that is associated with .class files in the explorer (via right click >> open with..)
#echo off
REM change to folder where the class file resides
cd %~d1%~p1
REM execute the class by calling its name without file extension
start java %~n1
The double-clickable JAR solution is the most common plain Java distribution method. There'd be a number of issues with trying to execute .class files directly, with the classpath the one that pops first to mind.
That said, if you wanted to support the very simplest possibilities in your development environment, you could conceivably implement a script that
inspected the .class file for the full class name (including package and inner class name)
walked up the directory tree to the root of the file's class path
(optionally included any common lib directories in the classpath)
invoked Java for the determined class
Then you could register your shiny script as a handler for .class files. But since you're in the development environment, aren't you happier with your IDE doing that?
For a .class file to run, needs in first place to have "something" to do, that is, that .class should contain a main method. Not all the .class do have one.
One thing you can do, is to wrap your app ( a number of .class files ) inside a jar file.
For short, you just need in addition to your classes a manifest file that says, where the main method is:
jar -cmf yourmanifestfile.mf doubleClickApp.jar *.class
And that's it, the doubleClickApp.jar is now executable with a "doubleClick" gesture.
When you install the Java Runtime Environment, it registers .jar files as an association in Windows. If you double-click on a .jar file, it will open it using Java. For this to work, you need to make sure you have a manifest defined that points to the class to run. Your class file to be run must have a main method that will be called.
Let's assume you have a class named 'com.TheClass.class' on disk. If you want to have this able to run with double click, create a file in a new directory called META-INF/manifest.mf. Put this into it:
Manifest-Version: 1.2
Main-Class: com.TheClass
Zip (or use the jar command) both your class up with this manifest directory and file. Rename it to mine.jar. Double click on it and it should launch your class with the Java runtime.
http://justaddhotwater.webs.com/javaexec.htm
This software makes it possible to run your Java classes by double-clicking them.(Windows ONLY).
The easiest way that I have found was creating a shortcut on the same folder than the .class file. Then right click on it and go to properties. Change the field Target to java NameOfClass, finally double click the shortcut :)