I am running my OpenCV project in eclipse successfully. I have added OpenCV-3.0.0 as my user library in my projects,but when I export a runnable jar file it can't be running in cmd windows (while the jar file of non-opencv projects run in cmd successfully). the [image below shows the error in running time of the jar file
I will be grateful for your guides.
You need to load the OpenCV lib pragmatically, you can try this:
public static void loadOpenCV_Lib() throws Exception {
// get the model
String model = System.getProperty("sun.arch.data.model");
// the path the .dll lib location
String libraryPath = "C:/opencv/build/java/x86/";
// check for if system is 64 or 32
if(model.equals("64")) {
libraryPath = "C:/opencv/build/java/x64/";
}
// set the path
System.setProperty("java.library.path", libraryPath);
Field sysPath = ClassLoader.class.getDeclaredField("sys_paths");
sysPath.setAccessible(true);
sysPath.set(null, null);
// load the lib
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
Now when your program initiated, you call the method at the first, then it will work.
Hope it solves the problem!
I have solved the problem by copying the opencv_java300.dll file in the folder D:\OpenCV3.0.0\opencv\build\java\x86 to the folder c:\windows\system32.
Related
I work with NetBeans IDE an I have a .txt file saved in src/myapp folder. If I run from the IDE, this recognise my
File file=new File("src/myapp/mytext.txt");
But if I build the jar file and double click it or launch it from command line I get this error:
java.io.FileNotFoundException: src\myapp\mytext.txt
I could insert absolute path, but how can I run my jar independently by the position of my project in the computer?
You can obtain the file path indepently of its position with the following:
ClassLoader classLoader = getClass().getClassLoader();
String path = classLoader.getResource("mytext.txt").toString();
Java is expecting to find the file relative to your working directory. So by hardcoding the file in src/myapp/mytext.txt you are expecting the user of your application to have the
file under the same folder structure.
If you are expecting the file to be at the same level of your jar file, you can just use ./mytext.txt. Do not put your mytext.txt under the src in your project. That is for sources you want to compile and/or bundle inside your jar file. In NetBeans move it outside the /src folder, that way when you run the program from your IDE or when you run it externally from your Jar file you find the file at the same level.
If you want the user to be able to specify the location himself of the file, you can also read the command line arguments (the arguments to your public static void main(String[] args)).
there is no such problem
File file=new File("./src/myapp/mytext.txt");
When loading the library in my main src folder there is no problem, but in the test src folder I get the error. I can still compile and run all the tests normally and they pass.
Both src folder are in the path and I got opencv as a library. Like I said, everything is working, so I guess it's a problem with Eclipse and the display of the error which should not be displayed? So the main problem is that it's visually a pain.
EDIT2 : I just want to say again that everything is working, the tests are all running, it is simply that they pop as problems (and I don't see the errors of the tests because this unspecifiedlinkerror is before and overshadow them)
Also, it does the same thing on both my Windows and Ubuntu machine.
my path is also correct when I print it out right before the System.loadLibrary as .../opencv-2.4.11/build/lib
EDIT3 : I tried Cibin William answer and put my .dll path but to no avail
You can right on the project and click on Build Path -> Configure Build Path -> then select the Libraries tab and select OpenCV jar file and then expend it and then select on Native Library Location and then click on the Edit and then brows the to the .dll file of OpenCV something like this C:\opencv\build\java\x64 Or C:\opencv\build\java\x86 for 32bit System. And it is that.
Or You can load the library by coding (dynamically)
public static void loadOpenCV_Lib() throws Exception {
// get the model
String model = System.getProperty("sun.arch.data.model");
// the path the .dll lib location
String libraryPath = "C:/opencv/build/java/x86/";
// check if system is 64 or 32
if(model.equals("64")) {
libraryPath = "C:/opencv/build/java/x64/";
}
// set the path
System.setProperty("java.library.path", libraryPath);
Field sysPath = ClassLoader.class.getDeclaredField("sys_paths");
sysPath.setAccessible(true);
sysPath.set(null, null);
// load the lib
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
When I run the OpenCV program in eclipse ,unsatisfiedLInkError occurs.I solve the error by exporting the library path in eclipse as follows
1.Right click on the project,seleect Debug as->Debug Configurations...
2.Debug Configuration window appears,Select the Environment tab in the top
3.Click the New button on the right side of the window,A New Environment Variable window appears
4.On the Name type LD_LIBRARY_PATH and in the Value type the folder which contain the .dll file(If the .dll is present in the Lib folder inside the project,type Lib in the Value)
Please try and reply.....
Well, I reinstalled Eclipse and that did it...sigh
The bug was on both Eclipse Luna and Mars Version 4.5.1.
Mars Version 4.5.2 is working fine.
I want to create an installer in java, that copy files from the source (like a packpage to put the files) to the Appdata folder, Is this possible? How can I make this?
String homeDir = System.getProperty("user.home");
String myAppFolderName = ".MyApp";
Path installDir = Paths.get(homeDir, "AppData");
if (!Files.isDirectory(installDir) { // Maybe not Windows
installDir = Paths.get(homeDir);
}
Path myAppFolder = Paths.get(installDir.toString(), myAppFolderName);
Files.createDirectory(myAppFolder);
Path sources = Paths.get(new URI("jar:file://... .jar!/install_image"));
Files.copy(sources, myAppFolder);
For a jar's File, URI:
MyAppClass.class.getProtectionDomain()
.getCodeSource().getLocation().toURI().getPath()
This uses
A fall back whenever there is no AppData directory (as on Linux or Mac)
Some subdirectory .MyApp to put everything in
A zip file system ("jar:file:/...") for the unpacking
A way to get the URI of a jar
You'll probably want to capture the case of running without jar too - for development.
This is a weird question . I want to know is there any way we could generate an apk from an android App. Let me explain- An android app which run cmd Commands and could generate apk for another application. Is there any possible way.
I have read that this could be done using ant in Java. I want to know that is this possible on a running android app.
Thanks.
You can use the ANT Jars ant.jar and ant-launcher.jar
in this case the path for build.xml should be fully specified and call it from java class this way :
public class AntTest {
public static void main(String[] args) {
String build = "D:/xampp/htdocs/aud/TempProject/build.xml";
generateApkThroughAnt(build);
}
/*
* Generate APK through ANT API Method
*/
public static void generateApkThroughAnt(String buildPath) {
File antBuildFile = new File(buildPath);
Project p = new Project();
p.setUserProperty("ant.file", antBuildFile.getAbsolutePath());
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
BuildException ex = null;
try {
p.fireBuildStarted();
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, antBuildFile);
p.executeTarget("clean");
p.executeTarget("release");
} catch (BuildException e) {
ex = e;
} finally {
p.fireBuildFinished(ex);
}
}
}
To create a build.xml file go to Eclipse=>Your Project=>Right click=>Export=>General=>Ant Buildfiles after that you will need to run :
android update project --name <project_name> --target <target_ID> --path <path_to_your_project>
Scripts can help you. Make a simple scripts which runs all android commands starting from compiling, packaging, creating key-store, signing using that key-store into release mode.
Android has provided everything which can work from command line.Just merge them in form of script and you will be able to generate .apk file.
You can make simple PHP script or ant script.
May be this is Possible, but You can do this by Reverse Engineering methods, so after getting source code of that apk file,compile it using Android IDE.
Reverse Engineering Strp by Step Method for apk decoding:
Step 1:
Make a new folder and put .apk file in it (which you want to decode). Now rename the extension of this .apk file to .zip (eg.: rename from filename.apk to filename.zip) and save it. Now you get classes.dex files, etc. At this stage you are able to see drawable but not xml and java files, so continue.
Step 2:
Now extract this zip apk file in the same folder (or NEW FOLDER). Now download dex2jar from this link http://code.google.com/p/dex2jar/ and extract it to the same folder (or NEW FOLDER). Now open command prompt and change directory to that folder (or NEW FOLDER). Then write dex2jar classes.dex and press enter. Now you get classes.dex.dex2jar file in the same folder. Then download java decompiler from http://varaneckas.com/jad and now double click on jd-gui and click on open file. Then open classes.dex.dex2jar file from that folder. Now you get class files and save all these class files (click on file then click "save all sources" in jd-gui) by src name. At this stage you get java source but the xml files are still unreadable, so continue.
Step 3:
Now open another new folder and put these files
put .apk file which you want to decode
download apktool v1.x AND apktool install window (both can be downloaded at the same location) and put in the same folder
download framework-res.apk file and put in the same folder (Not all apk file need framework-res.apk file)
Open a command window
Navigate to the root directory of APKtool and type the following command: apktool if framework-res.apk
apktool d "fname".apk ("fname" denotes filename which you want to decode)
now you get a file folder in that folder and now you can easily read xml files also.
Step 4:
It's not any step just copy contents of both folder(in this case both new folder)to the single one
and now enjoy with source code...
I don't think that's possible.
Because Android doesn't include the "compiler" inside itself. Well I guess If you really want to do it, you can always make a server the generate the apk file, and make your app connect with said server.
Hope that help you :).
Ok so I have a project in eclipse called SWT. In one of the classes in this project there is a code:
public static String rpath = "..\\SWT\\src\\data.txt";
public static String path = new File(rpath).getAbsolutePath();
If I compile and run my project in eclipse, my program is able to find data.txt and use it as input, which is the file's intended purpose.
I then export this project to an executable jar file, open the command prompt and enter java -jar SWT.jar. the program opens but in the command prompt, there is the following error:
java.io.FileNotFoundException: C:\Users\Yoshikawa\workspace\SWT\..\data.txt (The system cannot find the file specified)
My question:
How can I modify my code prior to exporting it to an executable jar, so that I can make my executable jar able to find the data.txt? By the way, my executable jar contains data.txt already, but is not able to use the relative path specified above to find its exact location, which I find very very odd.
There will be no src directory in your jar file. Only the package names and class files.
Try this:
URL url = ClassLoader.getSystemResource("/data.txt");
File file = new File(url.toURI());
More info here.
You can put the file, data.txt, at some package and then use a class from this package to do this:
URL url = ClassFromSamePackageOfFile.class.getResource( "data.txt" );
File f;
try {
f = new File(url.toURI());
} catch(URISyntaxException e) {
f = new File(url.getPath());
}