Embedding a Java (.jar) file within a Mac Application - java

I need to have a .jar file run within my main NSWindow, how can I do this?
I have been looking at Apple's example for "JavaFrameEmbedding", and seem to be able to run it without any Java exceptions (previously I had exceptions), however I cannot see the applet. They use the JavaFrameView which is part of the JavaFrameEmbedding framework, although I can't find any documentation about this at all. This is using Lion (I have also tried on Snow Leopard without success).

Download this puppy and dig through it.
If you want to make the NSWindow in java and have the program run from there try something like this:
import com.apple.cocoa.application.*;
import com.apple.cocoa.foundation.*;
public class Test {
public static void main (String [] args) {
NSApplication nsapp = NSApplication.sharedApplication();
NSRect rect = new NSRect(500, 500, 500, 500);
NSWindow.MiniaturizableWindowMask;;
NSWindow window = new NSWindow(rect, style, NSWindow.Buffered,
false);
window.makeKeyAndOrderFront(window);
nsapp.run();
}
}

Related

Why the exported to desktop (.exe) processing app with java classes can not run properly? My another project was exported without problems

I try to export my processing app as the "ready to use" application with exe launch file for desktop.
My code is pretty simple. Proccessing code:
//import io.itch.mgdsstudio.airfight.connecteddevices.*;
private io.itch.mgdsstudio.airfight.connecteddevices.Controller controller;
void setup(){
size(600,800, JAVA2D);
background(0);
//I think the next code line can not be launched after export. Background stays black
controller = new io.itch.mgdsstudio.airfight.connecteddevices.Controller(this);
}
void draw(){
controller.render();
}
and the java-class:
package io.itch.mgdsstudio.airfight.connecteddevices;
import processing.core.PApplet;
public class Controller {
private PApplet engine;
public Controller(PApplet engine) {
this.engine = engine;
}
public void render(){
engine.background(255,0,0);
}
}
The application runs perfect from processing ide - the screen is red. But after export it can not run properly. The screen is black. I tested processing 3 and 4. In january I exported an another application succesfully. But now I can not launch exported file. I think the trouble is in versions of the java source files.
I tried to change code so:
import io.itch.mgdsstudio.airfight.connecteddevices.Controller;
private Controller controller;
void setup(){
size(600,800, JAVA2D);
background(0);
controller = new Controller(this);
}
void draw(){
controller.render();
}
I receive the message in the console:
No library found for io.itch.mgdsstudio.airfight.connecteddevices
but it runs in the ide. But after export it can not run properly again. Maybe i need another package names?
There seems to be an issue/bug using package inside the Processing IDE.
As you hinted in your comments, if you could compile a .jar in your Java IDE of choice and use that in Processing that should work.
From a pragmatic point of view, if you don't plan on fixing the Processing IDE to handle package as expected, you can simply not use it your code:
import processing.core.PApplet;
public class Controller {
private PApplet engine;
public Controller(PApplet engine) {
this.engine = engine;
}
public void render(){
engine.background(255,0,0);
}
}
Controller controller;
void setup(){
size(600,800, JAVA2D);
background(0);
controller = new Controller(this);
}
void draw(){
controller.render();
}
This wouldn't be convenient on a larger project where you may want to write Java code in your preferred IDE but still be able to colaborate with others that use the Processing IDE alone.
Since running in the Processing IDE works (e.g. dev/testing), but can't export a .exe for easy deployment, a workaround could be using the processing-java command. (If you add your Processing installation folder to the Windows %PATH% environment variable you should be able to type processing-java in a new Command Prompt window and see the help/usage guide).
Running processing-java --sketch=path\to\YourSketchFolder --run should run the sketch the same way the Processing IDE does (without actually having the IDE open). While this implies installing Processing and adding it to %PATH% on any new machine on one hand, on the other it saves you the time of having rebuild/repackage new .exe files for every single change.

Put a program icon depending on the platform

I've realized that my code has a problem with setting a program icon (instead of the java one) in different platforms. I had written this code for windows and mac:
private void putIcon() {
URL url = ClassLoader.getSystemResource("resources/icon.png");
String name = System.getProperty("os.name");
if (name.startsWith("Win")) {
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
this.setIconImage(img);
} else {
Application.getApplication().setDockIconImage(new ImageIcon(url).getImage());
}
}
While I was testing it in mac there was not problem, but when I tried it windows I realized that won't work because that class isn't in windows:
import com.apple.eawt.Application;
What could I do to solve this issue? For what I've researched it's not possible to have some kind of "if" in the import section of the code, and if that class is there in windows it won't compile.
Check if the specified class Application can be found by using
Class.forName("com.apple.eawt.Application");
If this method does not throw a ClassNotFoundException, invoke the methods you want by using reflection only. Seems a bit hacky but it should work.
Also make sure that you are not importing the class.

UnsatisfiedLinkError in exported (Eclipse) executable jar file

The code works fine when executing from Eclipse. I'm using OpenCV 2.4.11 and JavaFX for UI. When I export an Executable Jar from Eclipse and run it from cmd I get the following exception:
I followed many post here on SO and OpenCV forum(1, 2, 3, 4) but, none of the answers seems to help me.
I have added the OpenCV jar as library and Native Library is linked to /build/java/x64 as suggested in SO answers.
The exception occurs at the System.loadLibrary(Core.Native_Library_Name), I checked the Native_Library_Name and the OpenCV version is same as the one I imported in my project.
public class CustomFrame extends Application{
#Override
public void start(Stage primaryStage){
Group root = new Group();
Canvas canvas = new Canvas(1440, 840);
ImageView imageView = new ImageView();
imageView.setFitHeight(canvas.getHeight());
imageView.setFitWidth(canvas.getWidth());
new FrameController().startCamera(imageView);
root.getChildren().addAll(imageView, canvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args)
{
// load the native OpenCV library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
launch(args);
}
}
If anybody thinks that I have missed something please do let me know.
The UnsatisfiedLinkError is thrown when an application attempts to load a native library like
.so in Linux,
.dll on Windows or
.dylib in Mac
and that library does not exist.
Specifically, in order to find the required native library, the JVM looks in both the PATH environment variable and the java.library.path system property.
Sometimes if the native library was already loaded by an application
and the same application tries to load it again, this can cause this
error also.
How to deal with the UnsatisfiedLinkError?
First of all we must verify that the parameter passed in the System.loadLibrary method is correct and that the library actually exists. Notice that the extension of the library is not required. Thus, if your library is named SampleLibrary.dll, you must pass the SampleLibrary value as a parameter.
Moreover, in case the library is already loaded by your application and the application tries to load it again, the UnsatisfiedLinkError will be thrown by the JVM. Also, you must verify that the native library is present either in the java.library.path or in the PATH environment library of your application. If the library still cannot be found, try to provide an absolute path to the System.loadLibrary method.
In order to execute your application, use the -Djava.library.path argument, to explicitly specify the native library. For example, using the terminal (Linux or Mac) or the command prompt (Windows), execute your application by issuing the following command:
java -Djava.library.path= "<path_of_your_application>" –jar <ApplicationJAR.jar>
You have missed the actual command. Use the following
java -Djava.library.path="C:\Opencv2.1.11\opencv\build\java\x64" -jar BlurDetector.jar
or
java -Djava.library.path="C:\Opencv2.1.11\opencv\build\java" -jar BlurDetector.jar
instead of your command
java -Djava.library.path="C:\Users\vivek_elango\Desktop" -jar BlurDetector.jar // you have given wrong path of your application
It looks like you need to add the path containing the opencv-2411 native libraries to the -Djava.library.path when running from the command prompt.
So something like this:
java -Djava.library.path="C:\Opencv2.1.11\opencv\build\java\x64" -jar BlurDetector.jar
In opposite to the other answers, I rather suggest you never use absolute paths, instead use relative ones. When you give your software to another user, the user most certainly won't have the libraries in the same path as you do. By using relative paths in regards to your application you guarantee that the software runs on other users systems as well, without them having to set path variables, jvm directives and what not. They don't even have to have OpenCV installed if you give them the library dll this way.
Here's code to load the libraries in a relative way:
public static void initOpenCv() {
setLibraryPath();
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("OpenCV loaded. Version: " + Core.VERSION);
}
private static void setLibraryPath() {
try {
System.setProperty("java.library.path", "lib/x64");
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
All you have to do is to
put the libraries into a lib/x64 folder relative to your jar file
in your application you have to invoke initOpenCv() at the start of your program
That's it. This way you can develop as before and maintain a distributable application.
Here's the full version:
import java.lang.reflect.Field;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.opencv.core.Core;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
initOpenCv();
HBox root = new HBox();
Label infoLabel = new Label();
infoLabel.setText("OpenCV loaded. Version: " + Core.VERSION);
root.getChildren().add(infoLabel);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void initOpenCv() {
setLibraryPath();
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("OpenCV loaded. Version: " + Core.VERSION);
}
private static void setLibraryPath() {
try {
System.setProperty("java.library.path", "lib/x64");
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public static void main(String[] args) {
launch(args);
}
}
With a folder structure like this:
.\application.jar
.\lib\x64\*.dll
Hint: I packaged the opencv jar into the application.jar

Why do I get "LoadLibrary failed with error 1114: a dynamic link library (DLL) initialization routine failed"?

When I run my java application program an error window appears saying that
"LoadLibrary failed with error 1114: a dynamic link library (DLL) >initialization routine failed".
I have tested my code on a different machine and it worked perfectly.The program shows a PApplet window with a map inside.However, Running the code on my laptop, the PApplet appears and all of the sudden the DLL error stops the rest from being shown.
What the problem could be and how can I fix it?
Here is the code I am trying to run. It is worth to mention that it runs successfully if I remove what's inside the setup() method.
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.providers.Google;
import de.fhpotsdam.unfolding.utils.MapUtils;
import processing.core.PApplet;
public class LifeExpectancy2 extends PApplet {
UnfoldingMap map;
public void setup()
{
size(800,600,OPENGL);
map = new UnfoldingMap (this, 50, 50, 700, 500, new Google.GoogleMapProvider());
MapUtils.createDefaultEventDispatcher (this, map);
}
public void draw()
{
}
}
I had the same issue after I installed my Netbeans to build some projects in PHP and it was fixed changing some graphics options in the control painel of my Windows 10.
Take a look on this video and see if it fixs your issue as well:
Windows 10 - Java Loadlibrary Error 1114
I hope it can be helpful!
Which Unfolding version did you download? You seem to use some Java IDE (and not Processing's one) so you need the Unfolding-for-Eclipse distribution which includes all needed native libraries (i.e. also the DLL in question).
For the records, the DLL is the native library for Windows OS to bind Java to the OpenGL API (JOGL).

fannj library doesn't work

I'm trying to run project which uses fannj library, but I'm getting error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'fann_create_standard_array':
at com.sun.jna.Function.<init>(Function.java:179)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:347)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:327)
at com.sun.jna.Native.register(Native.java:1355)
at com.sun.jna.Native.register(Native.java:1032)
at com.googlecode.fannj.Fann.<clinit>(Fann.java:46)
at javaapplication9.JavaApplication9.main(JavaApplication9.java:14)
Java Result: 1
This is what I did:
I put fannfloat.dll to C:\Windows\System32
I added fannj-0.3.jar to project
I added newest jna.jar to project
here is code:
public static void main(String[] args) {
System.setProperty("jna.library.path", "C:\\Windows\\System32");
System.loadLibrary("fannfloat");
Fann fann=new Fann("D:\\SunSpots.net");
fann.close();
}
SunSpots.net is file from example package. fannfloat.dll: you can get from here.
The "#8" at the end of _fann_create_standard_array indicates that the library is using the stdcall calling convention, so your library interface needs to implement that interface (StdCallLibrary) and it will automatically get the function name mapper applied that converts your simple java name to the decorated stdcall one.
This is covered in the JNA documentation.
It was the first time I had to work with FANN and it took me some time to make it work.
Downloaded Fann 2.2.0. Extract (in my case "C:/FANN-2.2.0-Source") and check the path of the fannfloat.dll file. This is the library that we will use later.
Download fannj-0.6.jar from http://code.google.com/p/fannj/downloads/list.
The dll is compiled for 32 bit environment. So, make sure you have a 32 bit Java installed (even in 64 bit Windows).
I suppose you already have the .net file with your ANN. Write something like this in Java
public class FannTest {
public static void main(String[] args) {
System.setProperty("jna.library.path", "C:/FANN-2.2.0-Source/bin");
Fann fann = new Fann("C:/MySunSpots.net" );
float[] inputs = new float[]{0.686470295f, 0.749375936f, 0.555167249f, 0.816774838f, 0.767848228f, 0.60908637f};
float[] outputs = fann.run( inputs );
fann.close();
for (float f : outputs) {
System.out.print(f + ",");
}
}
}

Categories