How to use javah - java

I've just wasted 2 hours trying to do something which I've already done twice before. I can't remember the exact procedure I used the previous two times, but it really shouldn't be giving me this much trouble:
I have a project folder called "BoardGUIv3". I want to produce a header file based on a class called "CANController", with the source located in "BoardGUIv3/src/model" and the class file in "BoardGUIv3/bin/model".
I've done exactly this thing before, but for some reason I can't seem to do this simple, one-line command again.
I'm pretty sure it's something along the lines of
javah -classpath <classpath> src/model/CANController
My classpath should just be the root directory, shouldn't it?

Here is the javah command usage:
{javahLocation} -o {outputFile} -classpath {classpath} {importName}
and it should be used like this for you class:
javah -o "CANController.h" -classpath "C:\pathToYourProjDir\BoardGUIv3\bin" model.CANController
Just correct the path to your classfile with the real path and the package structure if required.

Related

Stanford-NLP: Could not find main class error

This question seems to have been answered a few times (What does "Could not find or load main class" mean? and https://stackoverflow.com/a/16208709/2771315) but for some reason none of the shared methods are working.
What I've done so far.
1) Navigated to the directory containing the CoreNLP source files in terminal: ~/Downloads/CoreNLP-master/src
2) Selected one of the packages as a test case e.g. executed the command java -cp "*" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -file foo.txt (as per the docs, http://nlp.stanford.edu/sentiment/code.html)
I've tried variations of the above by altering the classpath -cp condition and setting it using set CLASSPATH = $CLASSPATH=~/Downloads/CoreNLP-master/src but can't seem to get a result. Does anyone know what I'm doing wrong? If I were to hazard a guess, I think that there is something wrong with the classpath but I'm not sure what.
The classpath should point to the classes, not the source files. If you're using the GitHub version of the code, you can set the classpath to be:
-cp ~/Downloads/CoreNLP-master/classes:/path/to/corenlp/models.jar
You can find the most recent version of the CoreNLP models at: http://nlp.stanford.edu/software/stanford-corenlp-models-current.jar (warning: >200MB file)
If you have one of the corenlp releases, you should set your classpath to:
-cp /path/to/corenlp.jar:/path/to/corenlp/models.jar
For example:
export CLASSPATH=stanford-corenlp-3.9.1.jar:stanford-corenlp-3.9.1-models.jar
Both the corenlp jar and the models jar should show up in the zipped release of the code (e.g., from http://nlp.stanford.edu/software/corenlp.shtml)
This worked perfectly fine for me.
java -cp "../*" -mx1g edu.stanford.nlp.sentiment.SentimentPipeline -file test.txt
Run this command while you are in the classes directory.
You are trying to run the program, instead of compiling it.

StandardAnalyzer does not exist

Based on this tutorial I tried to create my own basic Lucene search application. You can also use the given example directly, it has no bearing on my question.
So my file B.java begins with:
import org.apache.lucene.analysis.standard.StandardAnalyzer;
And although I have supplied the classpaths in javac,
javac -cp ~/lucene-4.10.2/core/lucene-core-4.10.2.jar
:~/lucene-4.10.2/analysis/common/lucene-analyzers-common-4.10.2.jar
B.java
I get errors like this:
B.java:1: error: package org.apache.lucene.analysis.standard does not exist
I use the plural because this also occurs for other packages where the .class is inside a given JAR (checked using unzip -l). I assume if I can find the solution for this, it should apply for the other cases as well... what am I missing out here?
OK, I seem to have found the answer.
For some reason, when including the ~ which expands to my $HOME directory, this makes javac unable to find the correct path:
javac -cp ~/lucene-4.10.2/core/lucene-core-4.10.2.jar
:~/lucene-4.10.2/analysis/common/lucene-analyzers-common-4.10.2.jar
B.java
I made a soft link to the directory instead:
$ ln -s ~/lucene-4.10.2/ lucenepath
Then using the symlink, it worked:
javac -cp lucenepath/core/lucene-core-4.10.2.jar
:lucenepath/analysis/common/lucene-analyzers-common-4.10.2.jar
B.java
Not sure why this is the case.

what is CLASSPATH in make file

I just got a makefile like this
CLASSPATH=.:/usr/share/java/antlr.jar
Mipsim.class: Mipsim.java MipsimLexer.class MipsimLexerTokenTypes.class MipsimParser.class Memory.class Processor.class
javac -classpath .:/usr/share/java/antlr.jar Mipsim.java
Memory.class: Memory.java MemoryAccessible.class
javac Memory.java
Processor.class: Processor.java
javac Processor.java
MemoryAccessible.class: MemoryAccessible.java
javac MemoryAccessible.java
MipsimLexer.java MipsimLexerTokenTypes.java MipsimParser.java: Mipsim.g
antlr Mipsim.g
MipsimLexerTokenTypes.class: MipsimLexerTokenTypes.java
javac MipsimLexerTokenTypes.java
MipsimLexer.class: MipsimLexer.java
javac MipsimLexer.java
MipsimParser.class: MipsimParser.java
javac MipsimParser.java
clean:
rm -f *.class MipsimLexer.* MipsimLexerTokenTypes.* MipsimParser.*
I have to run this make file, and build the object code.
However, the terminal told this
antlr Mipsim.g
make: antlr: No such file or directory
make: *** [MipsimLexer.java] Error 1
I guess probably anrlr.jar couldn't be found in this case. So I just changed the CLASSPATH into
CLASSPATH=.:/antlr.jar
and put antlr.jar in the same folder, then tried it again. The same error just happened again.....
Could somebody please help me out?
Thanks
CLASSPATH=.:/antlr.jar
That's not going to work. It says to use the current directory and the antlr.jar file in the root file system (which probably won't exist).
You may want to try:
CLASSPATH=.:./antlr.jar
And make sure that it exists for the duration of the antlr executable. Some make programs will execute each command in a separate shell so changing the CLASSPATH may not carry forward. I tend to use the bash "set variable for one process" method:
CLASSPATH=.:./antlr.jar antlr Mipsim.g
On top of that, the way you generally run antlr is by running java, giving the class you want to run, with something like:
java -cp ./antlr.jar org.antlr.Tool Mipsim.g
If you have an executable file (or script) antlr which will do that for you, it appears not to be in your path.

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.

compiling java from the command line

I have a package called studentServer which contains two sub packages student and common.
The common folder has references to the student package and i would like to be able to compile this. How could i do this?
javac student\*.java - compiles the student package
but when i try something similar with the common package errors are thrown - I understand it's something to do with the classpath
javac -verbose -classpath "\student" common\*.java
But I couldn't get this working. Any help would be great.
http://pastebin.com/m2a2f5d5d - here's the output from the compiler
This is a bit vague, but I suspect the classpath for the student code is wrong. Try without the leading backslash.
If you have a directory structure
source/
studentServer/
student/
common/
classes/
And you're in the directory above source, then you want to set the source path to 'source' with the -sourcepath option. You probably also want to use the -d option to tell javac where to put the compiled classes, so they aren't all mixed up with the source:
java -d classes -sourcepath source source/studentServer/student/*.java source/studentServer/common/*.java
go like this
c:\>
use change directory command cd until you get the desired directory
(ex: c:\javaEx\proj1\)
now
cd javaEx go like this
c:\javaEx\proj1\javac *.java
now compilation done in all java files in the proj1 directory.

Categories

Resources