I won't put all source code because it's really huge but I will try to explain my problem the best I can do.
I have an applet com.dmp.applet.DMPApplet, It's the main applet class and this one should be the first to load
com.dmp.applet.DMPApplet:
package com.dmp.applet;
// Imports
public class DMPApplet extends Applet
{
#Override
public void init()
{
this.state = AppletState.OFF;
this.running = true;
CPSAPI.connector = (Cpsw32) Native.loadLibrary("cpsw32", Cpsw32.class);
}
#Override
public void start()
{
CR_CPS cr = CR_CPS.fromShort(CPSAPI.connector.CPS_OuvertureSession(CPSAPI.pNomRessource, CPSAPI.pNomAppli, CPSAPI.pStatusService, CPSAPI.pNumSession.getReference(), CPSAPI.pFU.getReference()));
System.out.println("OUVERTURE DE SESSION : " + cr.getMessage());
this.jso = JSObject.getWindow(this);
if(this.state == AppletState.OFF && this.running)
{
this.Attente_Connexion();
this.Demande_Code_PIN();
}
if(this.state == AppletState.LOGGED && this.running)
{
this.Connexion_VS();
this.Lancement_Gateway();
}
if(this.state == AppletState.READY && this.running)
{
this.Ecoute_Evenements_CPS();
this.Fermeture();
}
}
public void stop()
{
CR_CPS cr = CR_CPS.fromShort(CPSAPI.connector.CPS_FermetureSession(CPSAPI.pNumSession.getValue(), CPSAPI.pStatusService));
System.out.println("FERMETURE DE SESSION : " + cr.getMessage());
}
public void destroy()
{
// TODO
}
}
It's architecture is the basis for an applet to execute but when I test it under Eclipse (Juno) but every time I launch the project I get :
java.lang.ClassNotFoundException: com.dmp.applet.DMPApplet.class
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:211)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:662)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:619)
Such a pain, I can't figure out what's happenning, the project works perfectly on another PC (the one my team mate use to develop), and it worked until two days ago...I have no clue...
The fact that there's a ".class" on the end here is suspicious:
java.lang.ClassNotFoundException: com.dmp.applet.DMPApplet.class
I'd expect this:
java.lang.ClassNotFoundException: com.dmp.applet.DMPApplet
How exactly are you launching it under Eclipse? (I've never written an applet in Eclipse.) If you have to specify the class anywhere, make sure you don't have the ".class" suffix, as it's not part of the class name.
Can you please make sure that your jdk is properly configured in your application build path on eclipse. Right click on your application on eclipse and goto build path. Select libraries and ensure the jdk is there.
Related
I currently try to run java code in Maxmsp using a mxj object, and I want to load some classes inside of the code.
But I always get the errors, although the code runs properly in eclipse.
What is the problem?
This is my code.
If I bang in Maxmsp, call() will be called.
package Load;
import com.cycling74.max.*;
public class Loaded extends MaxObject{
public static void main(String[] args) {
//This works properly in eclipse
call();
}
public void bang() {
//This should work in Maxmsp, but get errors
call();
}
public static void call() {
try {
//this is just a example
//I want to load some classes which locate the same directory as this class
Thread.currentThread().getContextClassLoader().loadClass("Load.Loaded");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
And this is the error message:
java.lang.ClassNotFoundException: Load.Loaded
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Load.Loaded.call(Loaded.java:21)
at Load.Loaded.bang(Loaded.java:16)
MXJ System class path is:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/commons-codec-1.11.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/core.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/gluegen-rt-natives-macosx-universal.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/gluegen-rt.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/jitter.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/jode-1.1.2-pre-embedded.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/jogl-all-natives-macosx-universal.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/jogl-all.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/max.jar:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/lib/sadamLib.jar
MXJ Classloader CLASSPATH is:
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/classes/
/Applications/Max.app/Contents/Resources/C74/packages/max-mxj/java-classes/
/Users/MyName/Documents/ecllipse-workspace/009_Processing/bin
Loaded.class is in /Users/MyName/Documents/ecllipse-workspace/009_Processing/bin
You need to include any of your dependencies on the classpath:
java -cp "path/to/maxmsp.jar;path/to/dependency2.jar;path/to/your.jar" classpath.of.your.Main
If you are just running directly from a classfile and haven't JARred your project then you can omit the path/to/your.jar and just run from the same directory with the classpath of your Main.
The above is for running java from command line.
Since Max is what is running and what's taking control of the classloading im guessing that sun.misc.Launcher$AppClassLoader is not working. Try debugging and see what it's doing. Also maybe try to find a way to use the Max classloader instead of the Java AppClassLoader.
The problem was what Max cannot load class properly.
So I created class loader method.
public static ClassLoader createClassLoader(String dirname) throws java.io.IOException {
java.net.URL[] url = new java.net.URL[1];
java.io.File file;
if (dirname.endsWith("/")) {
file = new java.io.File(dirname);
}
else {
file = new java.io.File(dirname + "/");
}
url[0]= file.toURI().toURL();
ClassLoader parent = ClassLoader.getSystemClassLoader();
java.net.URLClassLoader loader = new java.net.URLClassLoader(url, parent);
return loader
}
And call
ClassLoader loader = createClassLoader("ClassPath");
Class<?> c = Class.forName("Classname", true, loader);
I want to wrap a C++ library (PCL) in Java code using JNI, but I am having inconsistent results. I have first created a PointXYZ class for testing and it looks like this:
package pcl;
public class PointXYZ extends NativeObject {
PointXYZ() { }
#Override
public native void alloc(); // creates pointer + handle on the native side
#Override
public native void dispose(); // sets handle to 0 and deletes pointer
public native float getX();
// ...
}
I have generated the C header for this class using javah, compiled everything using CMake, tested it using its getters and setters and everything works perfectly.
static {
System.setProperty("java.library.path", System.getProperty("user.dir") + "/lib");
System.loadLibrary("pcl_java_common");
}
#Test
void attributeAccessTest() {
PointXYZ p = new PointXYZ();
p.alloc();
p.setX(3);
assertEquals(p.getX(), 3);
p.dispose();
// all is good
}
Now I have done the exact same steps for a PointXYZRGB class which inherits from PointXYZ and when I try to test that it throws java.lang.UnsatisfiedLinkError. Here is the class:
package pcl;
public class PointXYZRGB extends PointXYZ {
public PointXYZRGB() { }
#Override
public native void alloc();
#Override
public native void dispose();
public native short getR();
// ...
}
I have checked the generated .dll using Dependency Walker and the PointXYZRGB methods are all present. Anyone knows what the problem could be?
UPDATE: Here are the .dll functions as requested in the comment:
The problem was that System.setProperty("java.library.path", System.getProperty("user.dir") + "/lib"); does not actually make Java look for .dll files in the given path. It essentially does nothing. Then why do the tests work for PointXYZ? This is was my mistake of having put an older .dll into the project root folder, so it was essentially looking for methods in that.
I followed the instructions from this video https://www.codenameone.com/how-do-i---access-native-device-functionality-invoke-native-interfaces.html, but my code is throwing java.lang.ClassNotFoundException: interfaces.MyNativeImpl on the look up line. The code I'm using is almost exactly that of the video.
package interfaces;
import com.codename1.system.NativeInterface;
public interface MyNative extends NativeInterface{
public String sayHi();
}
in the android native directory
package interfaces;
public class MyNativeImpl {
public String sayHi() {
return "hi";
}
public boolean isSupported() {
return true;
}
}
and in the java code:
MyNative my = (MyNative)NativeLookup.create(MyNative.class);
if(my != null && my.isSupported()){
System.out.println("Hello!");
}
Where did I go wrong now?
That is perfectly fine code and will work on the device.
You are seeing the exception in the simulator because the native/internal_tmp directory is missing from your runtime classpath but it shouldn't cause a problem other than no native interfaces on the desktop:
I want to call a .DLL method in Eclipse. Here is my code :
class TestJNI1 {
public native void LireNoVersion();
public void a() {
System.loadLibrary("tttt.dll");
LireNoVersion();
}
}
public GlobalAction() {
this.setBackground(GlobalPreferences.PANEL_BACKGROUND_COLOR);
new TestJNI1().a();
}
The problem is that I have this error on compilation :
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: tttt.dll at java.lang.Runtime.load0(Unknown Source) at java.lang.System.load(Unknown Source)
I already tried to :
Set arguments in Eclipse
Moving at root of project and System32 folder
Added the folder path in native library location in Eclipse
Changing the %PATH% in windows
Giving the absolute path as an argument
Trying with "tttt.dll", "./tttt.dll" and ".tttt.dll"
Call with System.loadLibrary(...) and System.load(...)
UPDATE
I tried to print the java.library.path and get a path. I put the dll in this path and the error message is more confusing now :
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: D:\My\Exact\Path\tttt.dll: Can't find dependent libraries
Here is the code to print the path :
String property = System.getProperty("java.library.path");
StringTokenizer parser = new StringTokenizer(property, ";");
while (parser.hasMoreTokens()) {
System.err.println(parser.nextToken());
}
The first problem was it couldn't find the dll because it wasn't in the path.
The second problem is that it can't find the dependencies on the dll that you are using. Your choices would seem to be
Copy all dependent libraries to the dll location
Run your code from the original dll location.
Give the absolute path of the file
try {
System.load("C:/myapplication/application.dll");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
Using the Library interface in the sun.jna package did the trick :
import com.sun.jna.Library;
import com.sun.jna.Native;
public class DllTest {
public interface IDLL extends Library {
IDLL INSTANCE = (simpleDLL) Native.loadLibrary("tttt", IDLL.class);
int LireNoVersion(); // DWORD LireNoVersion();
}
public static void main(String[] args) {
IDLL sdll = IDLL.INSTANCE;
int nover = sdll.LireNoVersion();
System.out.println("version = " + nover + "\n");
}
}
Still don't know why it didn't worked before.
Try using this instead:
System.loadLibrary("tttt");
Request:
This is a very common problem faced by Java devs in my locale. I am really stuck for many days on this. Searched and tried a lot, read the docs. read ALL the stackoverflow questions related to JavaExe. Please only reply if you have done similar thing before and have a comprehensive answer. I would be really grateful to the community!
Senario:
I am using JavaExe to run an application as system service in desktop interactive capability.
To be exact I have an application that captures screenshots of desktops. I want it to run (as admin) on any user login so no one can stop it.
I have a myapp.jar, settings.txt and a lib dir.
I have searched alot and found JavaExe works (by watching its examples)
If anyone has a better way. Please state so.
Problem:
According to my research,
you must create a .properties file that has named like the .exe, and write "RunType = 1" in this file.
you define a static method in your main class : serviceInit()
Do I need to place any class or reference/import? How?
Edit:
My code below works as stand alone .jar and in javaExe.exe too.
It now does makes a system service and runs by as SYSTEM user. but It is NOT interactive to desktop. i.e its not showing any GUI.
package temp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class Temp {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
serviceInit();
}
public static boolean serviceInit(){
new Thread(){
public void run(){
Integer i = 0;
while(i < 999999999){
JOptionPane.showMessageDialog(null,i);
i++;
}
}
}.start();
return true;
}
}
And I dont think that bundling the .jar, lib directory and settings.txt into one .exe is possible?
you should have in your case :
public class MyApp_ServiceManagement
{
static boolean isMsgToDisplay = false;
/////////////////////////////
public static boolean serviceInit()
{
(new Thread()
{
public void run()
{
for(int i=0;i < 6;i++)
{
try { sleep(5*1000); }
catch(Exception ex) {}
isMsgToDisplay = true;
}
}
}).start();
return true;
}
/// is Data ready to be send to the UI ?
public static boolean serviceIsDataForUI()
{
return isMsgToDisplay;
}
/// Data to be send to the UI
public static Serializable serviceDataForUI()
{
isMsgToDisplay = false;
return "hello, I am an interactive Service";
}
}
and for the UI part :
public class MyApp_TaskbarManagement
{
/// To show (or not) the icon in tray
public static boolean taskIsShow()
{
return false;
}
/// Receive the message from Service
public static void taskDataFromService(Serializable data)
{
JOptionPane.showMessageDialog(null, data);
}
/// descr of UI
public static String[] taskGetInfo()
{
return new String[]
{
"UI part of Service"
};
}
}
the main() method is never called in service mode (except one particular case), but if you want keep your main() method you must put a call to main() in serviceInit().
Put serviceInit() in your main class or in another class named XXX_ServiceManagement where XXX is the name of your main class.
Then, serviceInit() must return before a 30 seconds delay.
Don't put a infinite loop, ... in it.
Put your code in a thread, and start it from serviceInit() (or main)
That answer to your problem?