I made a dll in C++ and wrote this class in java:
public class VolumeControl {
public native float GetVolume();
public native void SetVolume(float val);
public native void VolumeUp();
public native void VolumeDown();
public native void Mute();
static {
System.load("some_path/VolumeControl.dll");
}
}
it works good, if I call functions from this file, but when I'm trying to do this:
public class Server {
public static void main(String[] args) {
VolumeControl ctrl = new VolumeControl();
ctrl.Mute();
}
}
I get this:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
RemoteControl.VolumeControl.Mute()V
Both classes are in the same package, of course. How can I solve it? Thanks.
Update1: OK the problem was, that I added these classes to package. When I move them to default package, everything works good. But now if I want to use this dll with different packages, I need to rebuild it.
Update2: Actually I can't add it to package at all, when I'm trying: #javah VolumeControl, I get Error:
Could not find class file for 'VolumeControl'.
Update3: I added manually name of package to C++ functions and it works. Thanks.
The use of javah utility may help if integrated into a makefile to ensure the interface is always assumed on both side (client/server).
Related
So my first time using Eclipse doing an elementary program. I noticed that in Eclipse, you cannot compile a single class file. Rather you need to create a project on top of that. So I did create a project and created a class under the project. I noticed the code
package PackageName;
at the top of the class file. And if I delete the file and run the file, it gives me errors. May anyone answer me why is this happening? Thanks.
My code:
public class CSYes {
public static void main(String[] args)
{
System.out.println("Computer Science, Yes!!!!");
System.out.println("=========================");
}
}
Error Message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at proj1.CSYes.main(CSYes.java:3)
However, If I have
package proj1;
public class CSYes {
public static void main(String[] args)
{
System.out.println("Computer Science, Yes!!!!");
System.out.println("=========================");
}
}
It works perfectly fine.
The Eclipse IDE encourages you to use packages. In general, it's a good idea. I'd encourage you to use packages, too.
It is NOT, however, a requirement. It sounds like you inadverantly created a "proj1" package when you created the project and/or .java class. Whoops!
To fix the problem, simply a) delete the package reference in your .java source, then b) move the .java file OUT of "/src/proj1" and put in directly under "/src" (the "default package").
... OR, EASIER ...
Delete the entire source (both CSYes.java and proj1)
File > New > Java Class > Name= CSYes; leave package "blank" (i.e. "default package")
Copy/paste your code back into CSYes.
Voila! Done :)
I have looked through many answers to similar questions. But couldn't narrow down to a solution.
Following is the code: (Simplifying names for readability)
First class:
package p1;
public class C1 {
public static void test() {
System.out.println("Boom!");
}
}
Second class:
package p2;
import p1;
public class C2 {
public static void main(String[] params) {
C1.test();
}
}
Clean-Build doesn't give any error. (No compilation error)
But at runtime I'm getting following error:
Exception in thread "main" java.lang.NoSuchMethodError: C1.test()V
at C2.main(C2.java:6)
Java Result: 1
P.S. I'm using Netbeans.
This means that you are running your class C2 with an old version of class C1 in the classpath (a version that did not yet have the test() method).
Make sure you don't have old versions of C1.class somewhere. Remove all your *.class files and recompile everything, and then try to run it again.
Addition: As Kevin Bowersox noted in a comment, your main method must look like this:
public static void main(String[] args)
It must take a String[] as an argument.
It will properly compile and run only if main function will have String tab as args.
But also check versions of class C1 and C2, try rebuild your project to recompile that classes.
public static void main(String args[]) {
C1.test();
}
i think you should import it as
import p1.*;
Than you will get access to all classes and member functions in it.
Netbeans sometimes likes to get stuck after some changes and clean build doesn't work then.
Try editing each file that has been recently modified and saving it again (e.g. put a whitespace in a random place). After that, clean and build the project again.
If my memory refreshes and as Jesper pointed out, I also encountered that same issue NoSuchMethodFoundException under that same scenario (having still old class references that have not been cleaned).
I just copied your code snippets with 2 different packages directly in to my netbean, compiled and runned C2. It did print the BOOM! message.
In my case using :
public static void main(String args[]){
}
does not make a difference when I compiled and runned the code.
public static void main(String params[]){
}
It makes sense since the main class should have the correct method signature of main.
Here args or params, should not make a huge difference, I believe; as what we have inside the method is simply a reference for the inner body of the method that it uses.
Still definitely it is good practice to follow the standard signature for main.
I would recommend to clean the project and copy the contents from scratch in a new project and build it again, sometimes netbeans can go crazy.
I am trying to use a library that uses JNI.
I've tried the sample app provided by the developers and it works. So I know it's not a bug in the library.
I assume I'm doing something wrong in the process of importing the library:
copy the .so file into my libs folder (its called libjniRTSP.so)
copy the the jniRTSP.java (summarized below) into my project:
public class jniRTSP {
private volatile static jniRTSP libRTSP = null;
public static jniRTSP getInstance() {
if(null == libRTSP) {
synchronized(jniRTSP.class) {
if(null == libRTSP) {
libRTSP = new jniRTSP();
libRTSP.InitProductList();
// DEBUG
libRTSP.SetDebugView(1);
}
}
}
return libRTSP;
}
static {
try {
System.loadLibrary("jniRTSP");
} catch (Exception e) {
e.printStackTrace();
}
}
public native int GetBrandEnableRecorder();
public native int GetBrandEnableLocal();
public native int GetBrandEnableRemote();
...
then in my onCreate() I try to call one of the methods:
jniRTSP.getInstance().Init(.....)
Which returns the error:
UnsatisfiedLinkError: Native method not found com.myuniquepackage.jniRTSP.InitProductList:()I
UPDATE (FIX): instead of just copying the jniRTSP java file, I copied the whole package that contained it, keeping the same package name. I'm not sure if this fixed it because the package name was the issue, or if because it needed one of the other java files that were in that package. Although I'm pretty sure if it was a missing file, it would complain at compile time.
Fairly certain the package declarations have to be the same inside the C code as the Java code.
So the class jniRTSP should be in the com.myuniquepackage package in Java and have the native method InitProductList declared and the C code should have method declared as Java_com_myuniquepackage_jniRTSP_InitProductList
By moving the class you are probably breaking the link, change the package declaration in Java to match the demo project and see if it works, if it does you can change it back and then change it in the C code which is a bit more time consuming but easy enough.
I've been running different individual Java .java files in the Netbeans IDE by right-clicking the .java files themselves in the Project Explorer of Netbeans (the portion normally at the upper left part of Netbeans).
However, i've been googling on how to make a class file run another class file using code, but to no avail.
I have a project named "loadanotherfile" with 2 files, namely: Loadanotherfile.java and otherfile.java
I'm trying to make Loadanotherfile.java run otherfile.java, but I'm not exactly sure how. I read about Classloaders and URLClassloaders however these methods don't seem suitable for my purpose of running another .java file.
Below is the code of the 2 files i mentioned.
Loadanotherfile.java
package loadanotherfile;
public class Loadanotherfile {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
// TODO code application logic here
}
}
otherfile.java
package loadanotherfile;
public class otherfile {
public static void main(String args[])
{
System.out.println("This is the other file.");
}
}
I have a feeling that the task has something to do with using the "import" syntax (namely something like import loadanotherfile.* but even if my guess is correct, I'm still not sure on how to make my Loadanotherfile.java run otherfile.java using code.
How can I load otherfile.java using Loadanothefile.java?
Cheers
In Loadanotherfile.java
otherfile.main(args);
Compile the two together, and then from Loadanotherfile,
otherfile.main(args);
will do the trick. You don't need to import since you're in the same package. Note the linked tutorial.
I would investigate (however) class instantiation, and creating an instance of a new class to invoke upon. Invoking static methods from static methods isn't very OO.
Try This:
className.main(Args){
}
This works! ive tested it myself.
Check the public void main line. If there IOException and not there then insert
in Loadanotherfile.java
use this
otherfile.main(args);{
}
I love the Intellij IDEA but i have been stacked on one little problem with Java imports.
So for example there is a package with name "example" and two different classes in it: A.java and B.java.
And i wanna get access to class "A" from class "B" without imports. Like this:
class A:
package example;
public class A{ ... some stuff here ...}
class B:
package example;
public class B{
public static void main(String[] args){
A myVar = new A();
}
}
This code may not work, but it's doesn't matter. Trouble just with IDE and with its mechanism of importing classes.
So, problem is that i can't see A class from B. Idea says 'Cant resolve symbol' but i actually know that class A exists in package. Next strange is that complier works fine and there are no exceptions. Just IDEA can't see the class in the same package.
Does anybody has any ideas?
If they are in the same package, you can access A in class B without import:
package example;
public class B{
public static void main(String[] args){
A myA = new A();
}
}
Maybe this will help you, or somebody else using IntelliJ that is getting a "Cannot resolve symbol" error but can still compile their code.
Lets say you have the two files that buymypies wrote up, the standard Java convention is that the two files would exist in an Example directory in your source code area, like /myprojectpath/src/Example. But it is technically not a requirement to reflect the package structure in the source directory structure, just a best practice sort of thing.
So, if you don't mimic the package structure, and the two files are just in /myprojectpath/src, IntelliJ will give you the "Cannot resolve symbol" error because it expects the source code structure to reflect the package structure, but it will compile okay.
I'm not sure if this is your problem, but I do use IntelliJ and have seen this, so it's something to look at.
I have the same problem as this: 2 classes in the same package, yet when one tries to call the other, Intellij underlines it in red and says Cannot resolve symbol 'Classname', e.g. Cannot resolve symbol 'LocalPreferencesStore'.
It then wants to add the fully qualified name in situ - so it clearly knows the path - so why can't it just access the class?
The module still compiles and runs, so it's just the IDE behaving oddly - and all that red is very distracting, since it isn't actually an error, it's just IDEA throwing a weird wobbly.
This is also recent. Two weeks ago I wasn't having this problem at all, and now suddenly it's started up. Of course, it could go away again on its own soon, but it's really annoying.
Same issue and I just fixed it.
I don't know your folder structure.
However, if your package example was added manually.That's the problem.
The package should be the same as your folder structure,which means if you want your class file to be stored in package example,you must store you java file in the src's subfolder named example.
You need to learn the basics about Java i think.
Here is a basic example of what i think you are trying:
package Example;
public class A
{
String myVar;
public String getMyVar()
{
return myVar;
}
public void setMyVar(String myVar)
{
this.myVar = myVar;
}
}
You need to create an instance of A.
package Example;
public class B
{
/**
* #param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
A myA = new A();
myA.setMyVar("Hello World!");
System.out.println(myA.getMyVar);
}
}
Look up java 'getters' and 'setters'.