How to use the DexClassLoader? - java

I want to add .jar dynamically. So I make a demo. But I don't know how to new the DexClassLoader. I don't know how to add the first params.
final File optimizedDexOutputPath = new File("" + File.pathSeparator + "test.jar");
//PackageManager pm = getPackageManager();
String dexOutputDir = getApplicationInfo().dataDir;
DexClassLoader dexClassLoader = new DexClassLoader("", dexOutputDir, null, getClassLoader());
I have made the dynamical .jar as test.jar(become dex) as well as a new folder 'text' in projects and put the text.jar in it.
Can you help me see what I have done wrong?

Try this code:
// dexPath is the absolute path of your **DEX** file
ClassLoader loader = context.getClassLoader();
dexLoader = new dalvik.system.DexClassLoader(**dexPath**, dexOutputDir, null, loader);

The code to dynamically load a jar should look something like this:
//get the path to your .jar as a String
String jarPath = this.getApplicationContext().getFilesDir().getAbsolutePath();
jarPath += File.pathSeparator + "test" + File.pathSeparator + "test.jar";
//get a path to the directory you want to store odexs in as a String
String optimizedDir = this.getApplicationContext().getDir("odex", MODE_PRIVATE).getAbsolutePath();
//finally, call DexClassLoader
DexClassLoader dcl = new DexClassLoader( jarPath, optimizedDir, null, getClassLoader() );
The above assumes you've created a directory named "test" within your app's private files area and placed test.jar within that directory. You could create this directory and possibly copy test.jar from your app's assets area into this directory when your app first starts up.

Related

How to get spring boot application jar parent folder path dynamically?

I have a spring boot web application which I run using java -jar application.jar. I need to get the jar parent folder path dynamically from the code. How can I accomplish that?
I have already tried this, but without success.
Well, what have worked for me was an adaptation of this answer.
The code is:
if you run using java -jar myapp.jar dirtyPath will be something close
to this: jar:file:/D:/arquivos/repositorio/myapp/trunk/target/myapp-1.0.3-RELEASE.jar!/BOOT-INF/classes!/br/com/cancastilho/service.
Or if you run from Spring Tools Suit, something like this:
file:/D:/arquivos/repositorio/myapp/trunk/target/classes/br/com/cancastilho/service
public String getParentDirectoryFromJar() {
String dirtyPath = getClass().getResource("").toString();
String jarPath = dirtyPath.replaceAll("^.*file:/", ""); //removes file:/ and everything before it
jarPath = jarPath.replaceAll("jar!.*", "jar"); //removes everything after .jar, if .jar exists in dirtyPath
jarPath = jarPath.replaceAll("%20", " "); //necessary if path has spaces within
if (!jarPath.endsWith(".jar")) { // this is needed if you plan to run the app using Spring Tools Suit play button.
jarPath = jarPath.replaceAll("/classes/.*", "/classes/");
}
String directoryPath = Paths.get(jarPath).getParent().toString(); //Paths - from java 8
return directoryPath;
}
EDIT:
Actually, if your using spring boot, you could just use the ApplicationHome class like this:
ApplicationHome home = new ApplicationHome(MyMainSpringBootApplication.class);
home.getDir(); // returns the folder where the jar is. This is what I wanted.
home.getSource(); // returns the jar absolute path.
File file = new File(".");
logger.debug(file.getAbsolutePath());
This worked for me to get the path where my jar is running, I hope this is what you are expecting.
Try this code
public static String getParentRealPath(URI uri) throws URISyntaxException {
if (!"jar".equals(uri.getScheme()))
return new File(uri).getParent();
do {
uri = new URI(uri.getSchemeSpecificPart());
} while ("jar".equals(uri.getScheme()));
File file = new File(uri);
do {
while (!file.getName().endsWith(".jar!"))
file = file.getParentFile();
String path = file.toURI().toString();
uri = new URI(path.substring(0, path.length() - 1));
file = new File(uri);
} while (!file.exists());
return file.getParent();
}
URI uri = clazz.getProtectionDomain().getCodeSource().getLocation().toURI();
System.out.println(getParentRealPath(uri));

Resource File Path as String (not streaming)

I don't get it - I'm trying to get the path of a file so that the file (an image) can be included as an attachment in an email.
My system consists of two parts - a web app and a jar. (actually three parts - a common shared jar containing DAOs etc.)
They're both built using maven.
They both contain this image in this path:
src/main/resources/logo_48.png
WebApp:
String logo1 = getClass().getClassLoader().getResource("logo_48.png").getPath();
This works perfectly - both on local (Windows) and Linux
Jar Application:
String logo1 = getClass().getClassLoader().getResource("logo_48.png").getPath(); //doesn't work
I've taken advice from here:
How to access resources in JAR file?
here:
Reading a resource file from within jar
here:
http://www.coderanch.com/t/552720/java/java/access-text-file-JAR
and others
Most answers offer to load the file as a stream etc. but I'm only wishing to get the path assigned to the String. Other resources have led me to hacking the code for hours only to find the end result doesn't work.
After so many instances of /home/kalton/daily.jar!logo_48.png does not exist errors I was frustrated and settled on the following workaround:
Copy the logo_48.png directly to the folder where the jar resides (/home/kalton/)
Alter my jar application code to:
String logo1 = "/home/kalton/logo_48.png";
And it works.
Could anyone show me the right way to get the PATH (as a String) of a file in the resources folder from a JAR that is not unpacked?
This issue was driving me crazy for weeks!
Thanks in advance.
KA.
Adding actual use code of 'path' for clarity of use:
public static MimeMultipart assemble4SO(String logoTop, String emailHTMLText) throws MessagingException, IOException {
MimeMultipart content = new MimeMultipart("related");
String cid = Long.toString(System.currentTimeMillis());
String cidB = cid + "b";
String cssStyle = "";
String body = "<html><head>" + cssStyle + "</head><body><div>" + "<img src='cid:" + cid + "'/>" + emailHTMLText + "<img src='cid:" + cidB + "'/></div></body></html>";
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(body, "text/html; charset=utf-8");
content.addBodyPart(textPart);
//add an inline image
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.attachFile(logoTop);
imagePart.setContentID("<" + cid + ">");
imagePart.setDisposition(MimeBodyPart.INLINE);
content.addBodyPart(imagePart);
.............
From the top…
A .jar file is actually a zip file. A zip file is a single file that acts as an archive. The entries in this archive are not separate files, they're just sequences of compressed bytes within the zip file. They cannot be accessed as individual file names or File objects, ever.
Also important: The getPath method of the URL class does not convert a URL to a file name. It returns the path portion of the URL, which is just the part after the host (and before any query and/or fragment). Many characters are illegal in URLs, and need to be “escaped” using percent encoding, so if you just extract the path directly from a URL, you'll often end up with something containing percent-escapes, which therefore is not a valid file name at all.
Some examples:
String path = "C:\\Program Files";
URL url = new File(path).toURI().toURL();
System.out.println(url); // prints file:/C:/Program%20Files
System.out.println(url.getPath()); // prints /C:/Program%20Files
File file = new File(url.getPath());
System.out.println(file.exists()); // prints false, because
// "Program%20Files" ≠ "Program Files"
 
String path = "C:\\Users\\VGR\\Documents\\résumé.txt";
URL url = new File(path).toURI().toURL();
// Prints file:/C:/Users/VGR/Documents/r%C3%A9sum%C3%A9.txt
System.out.println(url);
// Prints /C:/Users/VGR/Documents/r%C3%A9sum%C3%A9.txt
System.out.println(url.getPath());
File file = new File(url.getPath());
System.out.println(file.exists()); // prints false, because
// "r%C3%A9sum%C3%A9.txt" ≠ "résumé.txt"
Based on your edit, I see that the real reason you want a String is so you can call MimeBodyPart.attachFile. You have two options:
Do the work of attachFile yourself:
URL logo = getClass().getLoader().getResource("logo_48.png");
imagePart.setDataHandler(new DataHandler(logo));
imagePart.setDisposition(Part.ATTACHMENT);
Copy the resource to a temporary file, then pass that file:
Path logoFile = Files.createTempFile("logo", ".png");
try (InputStream stream =
getClass().getLoader().getResourceAsStream("logo_48.png")) {
Files.copy(stream, logoFile, StandardCopyOption.REPLACE_EXISTING);
}
imagePart.attachFile(logoFile.toFile());
As you can see, the first option is easier. The second option also would require cleaning up your temporary file, but you don't want to do that until you've sent off your message, which probably requires making use of a TransportListener.

Unable to access/run executable jar placed within the project

I am trying to run an executable jar places in the resources folder of my project. If I place the jar in any directory of my File System and provide the absolute path, it works fine.
Please see the code below:
String jarPath = "C:\\JarFolder\\myJar.jar";
String command = "java -jar";
String space = " ";
String params = "-a abc";
try {
proc = Runtime
.getRuntime()
.exec(command + space + jarPath + space + params);
} catch (IOException e) {
e.printStackTrace();
}
But when I place the jar inside the resources folder, and set the relative jar path as:
String jarPath = "..\\..\\..\\resources\\myJar.jar";
I get an error: Error: Unable to access jarfile ..\\..\\..\\resources\\myJar.jar
I have verified the path, it is valid.
Am I doing something wrong here? Is this the correct way to do this?
Use the ClassLoader to get the path of your resource.
String jarPath = this.getClass().getClassLoader().getResource("myJar.jar").getPath();
String command = "java -jar";
String space = " ";
String params = "-a abc";
try {
proc = Runtime
.getRuntime()
.exec(command + space + jarPath + space + params);
} catch (IOException e) {
e.printStackTrace();
}
If this is being ran from a main static method, then just replace this.getClass() with YourClass.class.
Relative path should work straight forward. Need more details of the error description you see and project folder structure (where the executing jar is placed and where is the import folder placed.)
Other alternate solution is to find the absolute file location of the currently executing jar file. You can fetch it with below code snippet.
MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
Other jar file to be executed must be placed somewhere in the same folder or in some child folders.
Now remove the current jar file name from the absolute path and append the relative path of the other jar file and execute it. It should work.
If you are on window, you can use as follow. I have tested this code and it works.
public class test {
public static void main(String[]args) throws IOException{
String jarPath = test.class.getClass().getResource("/resources/b.jar").getPath();
System.out.println("jarPath "+ jarPath);
//the result path have extra "/" so we have to remove it as follow.
jarPath = jarPath.substring(1);
//and again the result is encoded so we need to decode it back
jarPath = URLDecoder.decode(jarPath);
Runtime
.getRuntime()
.exec("java -jar \""+jarPath+"\"");
}
}
Note: I put my runnable jar in my resources folder with name "b.jar".
And the above codes need some modification to meet your needs.
Good Luck

How to create a text file inside a specific directory?

I am trying to cretae a file SYS_CONFIG_FILE_NAME inside a specific directory SYS_CONFIG_DIR_NAME. using the below posted code, when i run the java program it creates two directories instead of one directory and one text file inside that directory.
The out put of the below code is
SYS_CONFIG/config.txt. But `config.txt` is not a text file it is just a directory named `config.txt`
i referred also to some question in stackoverflow but i could not find a solution. Please let me know what I am missing?
code:
private final static String SYS_CONFIG_DIR_NAME = "SYS_CONFIG";
private final static String SYS_CONFIG_FILE_NAME = "config.txt";
private static File newSysConfigInstance() throws IOException {
// TODO Auto-generated method stub
File f = new File(SYS_CONFIG_FILE_PATH + "/" + SYS_CONFIG_DIR_NAME + "/" + SYS_CONFIG_FILE_NAME);
f.mkdirs();
f.createNewFile();
return f;
}
I would do it that way, you have always to call createNewFile() to create a new instance of the file if it is not created.
File dir = new File(SYS_CONFIG_FILE_PATH, SYS_CONFIG_DIR_NAME);
f.mkdirs(); // this to create the directories need for your path.
File file = new File(dir, SYS_CONFIG_FILE_NAME);
if (file.createNewFile()) {
system.out.prinln("file first created");
}else {
// print a message here
}
return file;
You are telling it to make a directory of the form a/b/c if you want a directory of the form a/b then you should give it the directory you want it to create.
File dir = new File(SYS_CONFIG_FILE_PATH, SYS_CONFIG_DIR_NAME);
f.mkdirs();
return new File(dir, SYS_CONFIG_FILE_NAME);
You don't have to pre-create files before you use them.

copying in same directory of the project in java?

int reval = fcCari.showOpenDialog(this);
String sourcePath = fcCari.getSelectedFile().getAbsolutePath();
String targetPath = "C:\\Users\\nadzar\\Downloads\\Compressed\\JavaSQLite\\resource\\";
targetPath += fcCari.getSelectedFile().getName();
if ((reval == JFileChooser.APPROVE_OPTION)) {
File source = new File(sourcePath);
File target = new File(targetPath);
copyFile(source, target);
targetPathFoto=targetPath;
tambahFoto(targetPathFoto);
}
else{
JOptionPane.showMessageDialog(rootPane, "Batal Menambahkan Foto");
tambahFoto(this.targetFoto);
}
System.out.println(targetPathFoto);
If my project has moved, the path must be changed.
I ask how can the target path change while my project directory has changed?
Use properties file for this purpose, what you need to do is change the property value.
# app home, build absolute paths in code using this path
app.home=path_to_home_directory
Use java.util.Properties to read these properties.
See examples at mkyong.

Categories