NDK build in command line to rebuild my application - java

My project use native library. when I change the package name , I got following error.
code:
Native method not found:
com.nooshindroid.yastashir.controller.JNIServer.get_number_of_processors:()I
at com.nooshindroid.yastashir.controller.JNIServer.get_number_of_processors(Native Method)
at com.nooshindroid.yastashir.controller.JNIServer.runServer(JNIServer.java:27)
at com.nooshindroid.yastashir.game.FreebloksActivity.startNewGame(FreebloksActivity.java:520)
at com.nooshindroid.yastashir.game.FreebloksActivity$16.onClick(FreebloksActivity.java:774)
at com.nooshindroid.yastashir.game.ColorListDialog.onItemClick(ColorListDialog.java:79)
when I sreach the net I understand that I must rebuil my JNI folder in command line.
In command line I don't know how to give project path.
I read somewhere that I must write like this .
cd <project>
$ <ndk>/ndk-build
I don't know the steps to do that job.
it show my project path and when I write Dir , it shows me all of things that exist in my folder.
but nothing change.

WHen you use JNI, unless you specify otherwise, the package name is part of the function name it looks for in the C code. So if you change the package name in Java, you also need to change all your C code JNI function names.

As #GabeSechan has said, you need to edit the C or C++ code to match the new package name. When you change the package name, the directory structure changes. If you don't edit the C or C++ code to match, the methods won't be found.
You need to change your JNI methods from
Java_com_old_package_name_methodName
to
Java_com_new_package_name_methodName
See the JNI documentation for an example.
Rebuild your native code after renaming the methods.

Related

Compiling Error Java

So i have been working on my Project1 and For some reason i cant figure out why It wont run. I get the Error "Could not find the main class". What am i doing wrong?
My code is:
https://gist.github.com/anonymous/6604f427cc9d17391478
I'm not sure how to post all the code properly with out making it super confusing (I tried to figure it out earlier) But let me know if i can help!
Is there something wrong with my code? Or do i need to compile it in a certain way?
if your using eclipse, goto to run configurations, select:
Project: my Project1
Main Class: assignment1.Assignment1_test
this will work for sure :)
Let's say you have a folder/package assignment1 somewhere on your file system inside which you have your Assignment1_test and Fraction class.Refer the screenshot above to compile and run your code. :)
You have it in a package named assignment1. This means it is in a folder named assignment1. After compiling, go up to the folder that contains assignment1 then run java assignment1.Assignment1_test from there.
java expects a fully-qualified class name (the name of the class including the package). It also expects that the class is in your classpath (. is implicitly added). Packages are tied directly to the directory structure.
Combining that all together, since the full-qualified name assignment1.Assigment1_test must be specified to java, and since the package structure is the directory structure, then the class is expected to be in assignment1\ relative to the current directory and therefore you must be in the directory that contains assignment1 to execute it (unless it is somewhere else in your classpath, which, given your situation, I'm guessing is not the case).

Confusion on Uses of Jar Manifest File

I am reading packaging java applications with jar tool. I noticed a manifest file is created under META-INF directory. For a simple application, it felt like, it's serves no purpose. I searched on stackoverflow to understand the usages of Manifest file. I came across UsesOfManifestFile. While reading the answer, i got confused on point 2 and 3 which are :-
What are download extensions? I am not able to understand the concept in the answer.
What does it mean to seal the jar? It has given an example below like this
Name: myCompany/MyPackage/ Sealed: true
What is the use of putting such information? Can someone elaborate on these points.
Thanks.
The manifest file is like the guidance instructions for the java program to run the jar. When the manifest is created it will hold crucial information, for example a reference to the main program. When the java program runs your jar it doesn't know where to start, so it look sin the manifest for a line telling it where the class with the main method is so it has a start point for the program. Another use is a classpath line, which will tell the program where to find any 3rd party library's in the jar are, otherwise again, the java program won't find them.
There is a range of data that can be stored in the manifest file, I would recommend checking out the Oracle information on them and seeing if that clears it up a bit more.
EDIT: From the Oracle site regarding your example:
Packages within JAR files can be optionally sealed, which means that all classes defined in that package must be archived in the same JAR file. You might want to seal a package, for example, to ensure version consistency among the classes in your software.
You seal a package in a JAR file by adding the Sealed header in the manifest, which has the general form:
Name: myCompany/myPackage/
Sealed: true
The value myCompany/myPackage/ is the name of the package to seal.
Note that the package name must end with a "/".
What this appears to mean is that any and all classes that you use in your program must be within the same jar file.
EDIT 2 (For comment response)
A manifest may contain the following line:
Main-Class: com.mkyong.awt.AwtExample
When the javaw.exe (I think) runs your runnable jar, it has no idea where to start from, all java programs run via a main method, but if you have say 50 class files in your jar, it has no idea which one has the main method to start from. It will look at the manifest file, reads your above line and thinks right, the main method is in the package com.mkyong.awt and the class is AwtExample, it then finds this class and runs the program from that main method. It's most basic function within the jar is to tell the java program running the jar where to find the stuff it needs to run your jar.
Note: that example manifest entry came from a little tutorial which shows how to create a simple runnable jar.
Good Luck!

Add a new class to an existing JAR File(which contains source code)

I'll try to illustrate the problem as simple as I can.
I have a JAR file, which I extracted using Winrar. (The jar file contains an open source android library).
I want to modify this JAR file by adding a new class to the library.
So here are my steps:
First, I created a class using Eclipse and set the package name same as the android's library package name.
Second, I copied this java File to the folder of the other java files in the library.
Third, I tried to compile the JAVA file via the CMD using javac.
The path of the new java file and the other .JAVA and .CLASS files of the library is: C:\com\example\core\
The name of the new java file would be: "MyNewClass.java"
The command I run via the CMD is: javac C:\com\example\core\MyNewClass.java
But, during the compilation I get many errors saying: Cannot find symbols.
I've been looking up for a solution of this problem but couldn't figure how to solve it and make the new JAR File having another class that I created seperately.
What am I missing?
As per earlier comments:
Rather than trying to modify the JAR, you can get access to the full source code of the Universal Image Loader library by cloning the repository using git or hitting "Download ZIP" on the righthand side of the page you linked.
Once you have the source, import the library in your IDE. From there on you'll be able to build the whole thing from scratch, make any adjustments/modifications you like, etc.
Your classpath might be wrong or there might be some mistake in package name.
When a Java program is being compiled the compiler it creates a list of all the identifiers in use. If it can't find what an identifier refers to it cannot complete the compilation. This is what the cannot find symbol error message is saying, it doesn't have enough information to piece together what the Java code wants to execute.
Try:
javac -cp com/* C:\com\example\core\MyNewClass.java
That should make the compiler aware of all the other classes under com/...

Troubleshoot NoClassDefFoundError in Java

I have a Java program called Main.java, it is located in the following directory:
/home/user/program/Main.java
When I try to run Main.java from the 'program' directory, everything goes ok, I use this line:
/home/user/program$ java Main
But when I try to run Main.java from the home directory :
/home$ java /home/user/program/Main
I get :
Exception in thread "main" java.lang.NoClassDefFoundError: /home/user/program/Main
Caused by: java.lang.ClassNotFoundException: .home.user.program.Main
What is the cause of this error?
This is due to your classpath, which will default to the current directory. When you run java Main from /home/user/program it finds the class in the current directory (since the package seems to be unset, meaning it is the default). Hence, it finds the class in /home/user/program/Main.class.
Running java /home/user/program/Main from /home tries to find the class in the classpath (the current directory) which will look in /home/home/user/program expecting to find the file Main.class containing a definition of the Main class with package .home.user.program.
Extra detail: I think the java
launcher is trying to be nice by
converting /-notation for a classname
to the .-notation; and when you run
java /home/user/program/Main it is
actually running java
.home.user.program.Main for you. This
is because you shouldn't be specifying
a file, but a fully specified
classname (ie including package
specifier). And when a class has a package
java expects to find that class within a
directory structure that matches the package
name, inside a directory (or jar) in the
classpath; hence, it will try to look in
/home/home/user/program for the class file
You can fix it by specifying your classpath with -cp or -classpath:
java -cp /home/user/program Main
Because its looking for the class using the fullname you give (/home/user/program/Main). You should only look for the Main class but using the good classpath :
java Main -cp /home/user/program
Which means it'll search the Main class in the given set of paths
Your 2nd command version does not know where to find the classes.
You need to provide the so called classpath
/home$ java -cp userprogram Main
Because of what you say I conclude this:
Main is in "top" (root) package
And when you execute java you must indicate the classpath, it is, the root directory where your pakage and classes structure is located.
In your case it is the very /home/user/program. And I guess your classpath is defined as "." (the dir you are located at). When you call java from home the classpath is being taken erroneosly.
If you want to call your main using a different package declare the package at the top of the class:
package user.program;
And set the classpath to /home (or execute java from that dir).
Next call java this way:
java user.program.Main
using dots because its a full class name (indicating packages). That is translated to dirs concatenating classpath + package + class. By example:
/home
user.program -> user/program/
Main -> Main.class
Good luck!
The problem is that if you call java /home/user/program/Main the package Main is in is meant to be home.user.program, which I assume is not true for Main (I assume it's in the default package, i.e. none at all). Is there a package declaration at the top of Main?
I'd suggest to use the classpath suggestions in the other answers.
This works for me:
java -cp /home/user/program Main
just a while ago faced this kind of error of (NoClassDefFoundError). I imported some third party library in my android app using eclipse env. I got this error during a runtime - some class from this third party library couldn't be found and a result of this NoClassDefFoundError was thrown, despite the mentioned library correctly appeared in classpath, so I really didn't know what else can be done to solve this problem.
While playing with "Order and Export" tab within "Java Build Path", I put my imported third party library to the top of the list of all libraries in my project and checked its checkbox - this solved the problem
I came across this same error when trying to compile and run it. The book, "Head First Java" explains and addresses this problem appropriately. Here is a screenshot from the book for your reference.
Hope its helpful.

How do I use user defined Java classes within Matlab?

I have read the documentation and several websites on exactly how to do this, however Matlab does not seem to pick up the classes that I have added to the dynamic java class path. Nor do I use the right syntax to correctly construct the object.
I have an class HandDB and which to create an object of this type and invoke it's static methods to connect to a SQL database. The class has an empty constructor and takes no parameters. The class is part of a package 'nuffielddb' which I made in a project within Netbeans. All the files are on my usb stick which is my E:\ drive...
I would like to be able to use all the classes within the package. The package is contained at E:\nuffielddb.
I entered the following commands into Matlab:
javaaddpath('E:\');
javaclasspath; % Output from java class path includes E:\ within dynamic path
str = java.lang.String('Test'); % Works fine
db = nuffieldbd.HandDB(); % Does not work - undefined variable or class error
Interesting I typed 'import nuffielddb.*;' and received no error.
Just where am I going wrong?
Thanks for your help btw!
Ah problem solved! Well not solved in a sense! I found out it's actually a problem with my matlab installation and I have no idea how to fix it :-(
Never mind, it works on the computers at the office :-)
if your classes are in a .jar file, make sure your classpath includes the .jar file name itself (not just the directory it's in).
Also if the MATLAB JRE is Java 1.5 (R2006b is, whereas R2009a is Java 1.6, not sure when they switched), make sure your classes are compiled with 1.5 as a target, not 1.6, otherwise MATLAB will not be able to use them.
Minor note: .* imports will never error, so they're not diagnostic. They simply add a package to the list that Matlab searches through when trying to resolve a class name. Nonexistent packages are ignored.
>> import this.package.does.not.exist.*
>>

Categories

Resources