I am making a Software in whihch I have to display the pdf files. I have stored the pdf files in my project folder. The software runs perfectly fine.
But when I cleand and build the project and then i run my jar exe file the pdf files doesnt open.
After some experiments I included my pdf files in SRC folder and then clean and build the project. The difference i found is that now the jar file is bigger in size ( it equals to sum of all the pdf files) , I thought that this time it would work . But it didnt work.
Then After more experiments I included all the files in the Dist folder.
Then The jar files can open the pdf files :) , I was happy but not satisfied, Since I only have to create a jar file and seeing all the pdf files in the project folder with one jar files looks really awkard and senseless, is thier anyway i can open the pdf files using only the jar file without copying the pdf files in the folder where my jar file is stored, . This is the code i used to open a file named "aleemullah resume".
try{
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "aleemullah resume.pdf");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error");
}
It looks as if you are reading the PDF file from your current working directory (that is, the folder that your program is launched from). By that logic, you should be able to open a PDF stored anywhere by entering it's full path, rather than just the file name. For instance:
String filePath = "C:\Users\you\Desktop\aleemullah resume.pdf"
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + filePath);
Change the filePath variable to wherever you are storing the PDF file.
You might also consider using a JFileChooser to select the file if you want the user to choose the PDF during runtime. Check out this example of how to open a dialog box from Java to pick a PDF file and retrieve its path.
Related
Sitting here with a bigger java project (for a first year software student) having some troubles creating the correct file path to resources.
I right now have files in the form of .txt, .png, .jpg, and a .gif.
Right now i use paths like this to find a text file:
File userFile = new File("Source Code/files/users.txt");
Or paths like this to create an image loaded in my FX code:
File logoPath = new File("Source code/files/graphics/Streamy_logo.png");
Image logoImage = new Image(logoPath.toURI().toString());
logo.setImage(logoImage);
This works fine in my IDE (IntelliJ), however it doesn't work when i create the project as a Jar file.
I think is has to do with the "source code" directory not created in the jar-file, which makes sense now.
Tried to read different subjects, but it seems a bit different if i should use a getResources-method, set a resourceStream or something else.
Can anybody please help me with this.
Thank you!
You can't load files like that in .jar files here's an example on how to read BufferedImages.
But first, make sure you have marked the resource folder as a resource folder in IntelliJ by right-clicking the folder in the project view and going down to "Mark directory as" and checking resource root.
BufferedImage exampleBlock = null;
try {
exampleBlock = ImageIO.read(ClassLoader.getSystemResource("exampleBlock.png"));
} catch (IOException e) {
e.printStackTrace();
}
By using this method of getting files all your files will be implemented into the .jar file and you can use them by calling their file name + extension
For maven or gradle projects, that respects maven directories convention you can simply use
getClass().getResource("/your_file_located_in_src_main_resources.extension").getPath()
I know how to extract apk files to classes using a windows based system as below:
Step 1:Renaming .apk file
Rename the .apk file with the extension .zip (for example let the file
be "demofile.apk" then after renaming it becomes "demofile.apk.zip")
Step 2:Getting java files from apk
Now extract the renamed zip file in specific folder, for example let
that folder be "demofolder". Now Download dex2jar from the link for
windows and extract that zip file in folder "demofolder".
Now open command prompt and go to the folder created in previous step
and type the command "dex2jar classes.dex" and press enter.This will
generate "classes.dex.dex2jar" file in the same folder.
Now Download java decompiler from the link and extract it and
start(double click) jd-gui.exe
From jd-gui window browse the generated "classes.dex.dex2jar" file in
demofolder, this will give all the class files by src name.
Now from the File menu select "save all sources" this will generate a
zip file named "classes_dex2jar.src.zip" consisting of all packages
and java files.
Extract that zip file (classes_dex2jar.src.zip) and you will get all
java files of the application.
Above steps will generate java files but to get xml files perform
following steps.
Step 3:Getting xml files from apk
Download apktool and apktool install from the link and extract both
files and place it in the same folder (for example "demoxmlfolder").
Place the .apk file in same folder (i.e demoxmlfolder)
Now open command prompt and goto the directory where apktool is stored
(here "demoxmlfolder") and type the command "apktool if
framework-res.apk" Above command should result in "Framework installed
..." Now in command prompt type the command "apktool d filename.apk"
(where filename is name of apk file) This will generate a folder of
name filename in current directory (here demoxmlfolder) where all xml
files would be stored in res\layout folder.
But I would like to know how to accomplish the above programatically within Android. So my android application can simply extract another app's dex file. I don't mind if the dex file is copied elsewhere on the phone, then decompiled (it is simply the decompilation I'm stuck on)
On the principle as below, that I have access to each app's dex, just not the knowledge of how to decompile it within Android.
Any help would be fantastic.
final PackageManager pm = getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo("PACKAGE NAME HERE", PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File(packageInfo.applicationInfo.sourceDir);
Uri uri = Uri.fromFile(file);
You can browse the sourcecode of the ShowJava application here. It looks like he is using the the Dex2Jar tool. The decompilation starts in a BackgroundService and all the magic happens in some special Jar classes. I do not know if this code comes from a library or if all this code is created by him. Look at the ExtractJar and the DecompileJar method than it shouldn't be to hard to understand what's going on.
You need to follow some steps in order to get it working:
Got to the dex2jar bitbucket site and download the files. Add the jar files to your project.
Via the DexFileFactory, you can load the apk, convert it to dex (loadDexFile) and save it to a file (writeDexFile).
With another library that converts jars to java-files, you can finally read the content. I found Procyon, which seems really simple. Load the class file with an InputTypeLoader and then call Decompiler.decompile().
Of course you can use other libraries in step 3, but for step 1 and 2, I haven't found any alternatives.
I have a project in eclipse
foo_project
- src
- bar_package
bam.java
info.txt
- info.txt
- resources
- info.txt
In bam.java, say, I print the content of info.txt out like
try {
welcome = new BufferedReader(new FileReader("src/info.txt"));
String currentLine = null;
while ( (currentLine = welcome.readLine()) != null) {
System.out.println(currentLine);
}
welcome.close();
} catch (Exception fof) {
System.err.println(fof.toString());
}
It is working inside eclipse as it is when I put info.txt under src folder, however, it doesn't work once I export this project to a JAR file.
In the code, I tried just "info.txt" as well as "src/info.txt", none of them is working! As you can see, I put info.txt pretty much everywhere and not successful!
How can I refer to this info.txt in the Java code, and make Java find it at both inside eclipse and JAR file?
If the text in your info.txt will always be the same, instead of treating the text as a file, consider treating it as a "resource". If you do that , you can include it within your JAR, instead of having to distribute it as a separate file.
You open an InputStream to a resource using the Class.getResourceAsStream() method.
I think to load files from Jar file the file reader method will not work effectively, you will have to use class.getResource() or class.getResourceAsStream() methods
some helpful links,
Load a resource contained in a jar
How to load resource from jar file packaged in a war file?
Load resource from class's own JAR file
Also as others have suggested make sure the Jar file contains the txt file you looking for, you can either use "jar" command or winRAR
To access resources inside a JAR file, you need to use
YourClass.getClassLoader().getResourceAsStream("info.txt")
This way it will look inside the JAR file (or rather, in all places on the classpath) for the file as a Resource. It will work in both Eclipse and when packaged as a JAR.
I ended up using in the class
getClass().getResourceAsStream( "/info.txt") which gives an InputStream
Then I use InputStreamReader and BufferedReader to read out the file.
/ here is the src folder. Everything under this folder will be built to bin folder at the end.
If you have multiple source folders (a folder can be set to be source folder by right click and choose that option in Eclipse), all source folders built to single bin folder
For example,
if you have source folders
sourceA
foo_package/...
sourceB
bar_package/...
then in bin, it will be
bin (this is the "/")
foo_package/...
bar_package/...
Thanks for all the answers and inspiration to all!
you must put your txt file next to the jar file in your jar folder
it means that copy your txt file into your jar file folder not into jar file
I have "help.html" file in resource directory. I can open it from Eclipse these ways:
As chm file:
Runtime.getRuntime().exec("hh.exe res/help.html");
As html file by default system browser:
URL resource = getClass().getClassLoader().getResource("help.html");
File file = new File(resource.toURI());
Desktop.getDesktop().open(file);
When I create executable jar file I cannot open "help.html" anymore. Is there any way to open the file from jar archive? I know I can save "help.html" outside jar and open it but this is not what I want. I want to have a single jar file.
You can't do that. This is beacause the system (hh.exe or the default browser) can't open a file that is stored inside your JAR.
The best solution IMHO is to copy the resource to the system temporary folder, and then run either command to open it. This way, your file is still stored inside the JAR, but can be copied to a location outside of it just when it needs to be opened.
I want to add a picture to my GUI program created using Eclipse and MyEclipse (for GUI visual design) from the resource pictures I pasted earlier in the project.
I managed to load pictures that lies just beside the .JAR file using
image = ImageIO.read(new File("imageFile.jpg"));
But I want to use the image from my resources "src" folder directly , so that the .JAR file is a standalone file yet loads pictures nicely.
I tried to make it
image = ImageIO.read(new File("src/ldtlogo3.jpg"));
I use this method when exporting the .JAR file
Java: export to an .jar file in eclipse
Use the overloaded ImageIO.read method taking an InputStream as a parameter, and use MyClass.class.getResourceAsStream() to get this input stream. getResourceAsStream loads a resource from the classpath (and thus from the JAR of your application). Its api doc will tell you which path it expects.
Note that the src directory is used to hold your Java source files. The jar doesn't contain it. It contains the .class files, in a hierarchy which directly maps the package hierarchy. Eclipse will automatically "compile" the image file by copying to the output directory, along with the .class files.