Use javah to generate header file - java

i am triyng to generate header file for native use (c/c++) from a java file using eclipse.
from the command line i can only reach the javah when i am in this location :
C:\Program Files (x86)\Java\jdk1.7.0_51\bin>
but i cant reference my java class like so :
C:\Program Files (x86)\Java\jdk1.7.0_51\bin>javah -jni com.or.jnihelloworld.nativeclass
because the class located outside of this folder at :
C:\Users\Or Azran\workspace\JniHelloWorld\src\NativeLib.java
and i want to make this file in to a jni folder in :
C:\Users\Or Azran\workspace\JniHelloWorld\jni
how can i do it from command line?
a good toturial will be also great

There appears to be a couple of issues to solve here.
First. I'm not sure that your source code is set up correctly. If indeed your class is com.or.jnihelloworld.nativeclass then it should be in directory: C:\Users\Or Azran\workspace\JniHelloWorld\src\com\or\jnihelloworld\nativeclass.java
However, assuming that the class/directory is correct. The javah command uses -d to specify the output directory, and you can specify the path with -classpath so
javah -classpath "C:\Users\Or Azran\workspace\JniHelloWorld\src\" \
-d "C:\Users\Or Azran\workspace\JniHelloWorld\jni" com.or.jnihelloworld.nativeclass
should put the file where you want it.

Your PATH doesn't include the bin directory of the JDK.

Related

How to use a .jar from Command Line without typing full path

I downloaded example.jar and I can type java -jar example.jar from within the directory where it is located and it works.
The problem is that I need to be able to call it from elsewhere without typing the full path. Is it possible?
I tried adding it to $CLASSPATH like this:
export CLASSPATH=$CLASSPATH:/Path/to/Directory:/Path/to/Directory/example.jar with no success.
Yes. Option 1. Using the CLASSPATH you have set, however you would have to specify the fully qualified main-class from the jar
java com.mypackage.MyMain
As long as com.mypackage.MyMain is on the CLASSPATH and contains a valid main method, that will run it.
Option 2. Create a bash shell script to run it (note that this is really providing the full path to the java command)
#!/usr/bin/env bash
export JARFILE="/Path/to/Directory/example.jar"
java -jar $JARFILE

what paramter must be used to pass the path of class file to be executed in java code

java -Xms512m -Xmx512m -Djava.util.logging.config.file="$FILE"\
-classpath "$HH_CLASSPATH" home.bitsbridge.Desktop.HPPP.HomeHealthGrouper.dist.com.mmm.cms.homehealth.test.HomeHealthGrouper_HP\
"input=$TEST_FILE" "config=$BASEDIR/config/HomeHealthGrouper.properties"\
$OPTIONS
As mention above,I have this java code embed inside shell code:
#!/bin/bash
BASEDIR=/home/bitsbridge/Desktop/HPPP/HomeHealthGrouper
FILE=/home/bitsbridge/Desktop/HPPP/HomeHealthGrouper/config/logging.properties
export HH_CLASSPATH=.:/home/bitsbridge/Desktop/HPPP/HomeHealthGrouper/dist/HomeHealthJava.jar:.:$BASEDIR/dist/HH_PPS_V_API.jar
#JAVA_VERSION="$(java -version)"
export JAVA_VERSION
export TEST_FILE=$BASEDIR/TestData/TestDataV7118.txt
export OPTIONS=1
java -Xms512m -Xmx512m -Djava.util.logging.config.file="$FILE"\
-classpath "$HH_CLASSPATH" home.bitsbridge.Desktop.HPPP.HomeHealthGrouper.dist.com.mmm.cms.homehealth.test.HomeHealthGrouper_HP\
"input=$TEST_FILE" "config=$BASEDIR/config/HomeHealthGrouper.properties"\
$OPTIONS
export BASEDIR=""
export HH_CLASSPATH=""
export TEST_FILE=""
export OPTIONS=""
So I need to run this file but i want to know what parameter to use for providing the class file:
Home.bitsbridge.Desktop.HPPP.HomeHealthGrouper.dist.com.mmm.cms.homehealth.test.HomeHealthGrouper_HP
as it is giving the error:
Error: Could not find or load main class home.bitsbridge.Desktop.HPPP.HomeHealthGrouper.dist.com.mmm.cms.homehealth.test.HomeHealthGrouper_HP
Is there any parameter i might be missing before providing the class to be executed.
If i am replacing -classpath with -jar:
java -Xms512m -Xmx512m -Djava.util.logging.config.file="$FILE"\
-jar "$HH_CLASSPATH" /home/bitsbridge/Desktop/HPPP/HomeHealthGrouper /dist/com/mmm/cms/homehealth/test/HomeHealthGrouper_HP.class\
"input=$TEST_FILE" "config=$BASEDIR/config/HomeHealthGrouper.properties"\
$OPTIONS
It is giving the error:
Error: Unable to access jarfile .:/home/bitsbridge/Desktop/HPPP/HomeHealthGrouper/dist/HomeHealthJava.jar:.:/home/bitsbridge/Desktop/HPPP/HomeHealthGrouper/dist/HH_PPS_V_API.jar
The last argument of the java command would be the class to execute
You specified main class as home.bitsbridge.Desktop.HPPP.HomeHealthGrouper.dist.com.mmm.cms.homehealth.test.HomeHealthGrouper_HP. I think it is little mistake.
Instead add directory /home/bitsbridge/Desktop/HPPP/HomeHealthGrouper/dist/ to variable HH_CLASSPATH which is contains classpath and specify main class as com.mmm.cms.homehealth.test.HomeHealthGrouper_HP. And make shure that exists compilled class file /home/bitsbridge/Desktop/HPPP/HomeHealthGrouper/dist/com/mmm/cms/homehealth/test/HomeHealthGrouper_HP.class. It must be *.class file, not *.java file. *.java files contains source code and *.class files contains compilled binary code.
Thanks for the effort guys i figure it out:
java -Xms512m -Xmx512m -Djava.util.logging.config.file="$FILE"\
-classpath "$HH_CLASSPATH" com.mmm.cms.homehealth.test.HomeHealthGrouper_HP\
"input=$TEST_FILE" "config=$BASEDIR/config/HomeHealthGrouper.properties"\
$OPTIONS
The problem was that the jar file inside classpath was extracted and i only need to remove the extracted folder and mention .jar file path in class path and .class file path after that in com.mms.... form.

how to run a java class in another folder with jar file

i'd like to run a java class on other folder, i have a mysqlcon.jar on the current path and a PetsGUI.class on ./classes/
when i try to run it by doing
java -cp .:mysqlcon.jar -d classes/PetsGUI
i receive
classes/PetsGUI not found
if i move mysqlcon.jar on classes and type
java -cp .:mysqlcon.jar PetsGUI
on classes/ it runs, so the code is correct. what's the correct command to run it?
So you need the jar file, and the classes directory in the classpath:
java -cp ./classes:mysqlcon.jar PetsGUI
java doesn't expect a file path as argument. It expects the fully quelified name of a class. And that class is then searched on the classpath.

Generate JNI header file in different packages

If I have all my .java and .class files in one place (i.e. in the default package) then everything is OK and I do all the JNI stuff, etc.
But in this case I have package-ception (lots of directories), my class and Java files are separated in /bin and /src and so on. And I need to generate the header file, but I am getting errors all the time. I tried so many commands, I saw different tutorials. I am already out of options.
So my project is in c://gvk/SEP3 and then the class and Java files with the native methods that I am gonna use are in /bin/CalculatorServer and /src/CalculatorServer
I have all the time run the javah command from the directory where the class file with the native methods is. The commands I tried so far are:
javah -d ./CalculatorServer NativeMethodsCalculator
Error: Could not find class file for 'NativeMethodsCalculator'.
javah -d ./CalculatorServer CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'CalculatorServer.NativeMethodsCalculator'.
javah -d c://gvk/SEP3/bin/CalculatorServer -classpath c://gvk/SEP3/bin/CalculatorServer NativeMethodsCalculator
Error: Could not find class file for 'NativeMethodsCalculator'.
javah -classpath c://gvk/SEP3/bin/CalculatorServer -o NativeMethodsCalc.h src.CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'src.CalculatorServer.NativeMethodsCalculator'.
javah -jni bin.CalculatorServer.NativeMethodsCalculator
Error: Could not find class file for 'bin.CalculatorServer.NativeMethodsCalculator'.
What you didn't try: go just to /bin/ (not into CalculatorServer) and run
javah -jni CalculatorServer.NativeMethodsCalculator
This is the only way how to run it. Just look at the javah doc. It says "fully-qualified-classname" in the synopsis. "Fully qualified" means full classpath. You were giving it only the classname. It worked for you so far only because you were using a default package, which means that your fully qualified classname was equal to a bare classname.
Option -d and -o doesn't influence the class lookup, only the storage of native result. All the variants you tried do not make any difference to your mistake.
I have all the time run the javah command from the directory where the class file with the native methods is
That's your mistake. You should run it from the directory that contains the outermost package, with the inner packages and their .class files below it. Then you don't need a -d argument or a -classpath argument. Assuming your outermost package is CalculatorServer, you should be in the directory containing CalculatorServer, and the command line required is javah CalculatorServer.NativeMethodsCalculator.

Java: how to designate a dir so I don't need to specify it in order to reference its .jar files

Let's say that I have a jar file # /usr/local/apps/app1/java/file.jar. I would like to be able to reference file.jar in the java command without typing the full path. For example, java -jar file.jar. How can I achieve this?
You can't; -jar requires a jar file argument.
You could create a symlink, or provide a launcher script.
ln -s /usr/local/apps/apps1/java/file.jar file.jar would create a symbolic link to the jarfile, which might permit the JVM to run the jarfile. Of course, this would only work within the directory containing the symlink.
There is likely no way to do this generically without using a script.

Categories

Resources