Using javah -jni with an Eclipse project structure - java

I need to know if I'm doing the things in a wrong way.
I have the following project structure (a pretty standard one):
then I've configured javah as external tool like this:
When I run the external tool on OSManager4Windows.java I was expecting to find it_univpm_quickbackup_utils_OSManager4Windows.h in bin/it/univpm/quickbackup/utils/ but it is inside bin. Is that correct? Shouldn't be inside the same directory of the .class file?

The problem is that javah generates the header file on the directory that the command was executaded (which you specified the bin folder). It makes sense to generate all headers in the same folder because usually c/c++ project put all of headers in the same folder.
However, if you want specific folders, the following command generate the headers in the specific src folder.
-d "${workspace_loc}${system_property:file.separator}${container_path}" ${java_type_name}

Related

RobotFramework cannot import Java keyword library

I did some robot framework python examples with pybot, and referenced .py files as my library files. The folder structure I used was test/lib/myLib.py and test/test/myTest.robot, where /test was at the same level as the /src folder in my project in eclipse.
I then tried to do similar things with a java project using jython. I have /test/test/myTest.robot which imports the library ../lib/myLib.java. I even tried importing this file using RIDE, but it always shows up as red text, meaning the import failed. The specific message I get from using jybot on the command line is:
"Importing test library 'C:\Users\cody\git\myProject\test\lib\myLib.java' failed: ImportError: No module named myLib"
I read that I might need to add it to classpath, and I think in order to do so, I need to make it a .jar file. I'd rather not do all that if possible to just leave it as a .java file. I attempted to add the lib folder to the build path... By that I mean I added the /test/lib folder to the "Source folders on build path". I also exported the darn thing as a jar and added that as a library. All this was done from the project properties in Eclipse.
I'm pretty stuck... any ideas how to get this working?
Java being a compiled language, you need to compile your java Class before importing it with RobotFramework.
Normally, Eclipse would do that for you, and put the resulting .class files inside a bin repository. If not, you can issue the javac path/to/file.java command, and move the resulting .class file where you want it (somewhere referenced by the classpath.
From within the .robot file, you should have the line Library test/lib/myLib, without neither .java nor .class at the end.

Create a jar file using compiled class files and an existing MANIFEST.MF file

Is it possible to take existing .class files and a MANIFEST.MF to create a jar file?
Is there a library that can create a "valid" jar-file? I tried it manually and it didn't work (using 7zip).
ERROR: "Invalid or corrupt jar file"
If everything has been compiled before, it should (in my understanding) theoretically work, if you create a new zip file, put all the files in it in the original structure and then rename it to "jar".
My idea is to program something like this with java code. A solution where I could add a file to an existing jar, would also be ok.
If you're interested in why I want to use this, look at my initial question: Compile javacode out of a running java accpilaction - on a system that hasn't JDK installed
Well Jar -cf
Try the jar command in $JAVA_HOME/bin
$JAVA_HOME is the path to you JRE/JDK installation

Linking sourcefiles to compiled files within Eclipse

I'm trying to build a jar from an existing project. Since I want to be able to lookup the sourcecode within Eclipse (debugging etc.), I need to link the source files. I compiled the sources with this expression:
javac -d classes/ -s src/ -verbose -g -sourcepath Framework/ src/DivaAPI.java src/DivaCallBase.java
Afterwards I run this commands:
cd classes
jar -cvmf manifest.txt Framework.jar package/name/*.class > nul
My directories look like this:
root
src
.java files
classes
package/name/.class files
manifest.txt
Afterwards, the generated jar-file is copied to the lib ordner within my project, import it in the library tab (properties/Java Build Path/Library ) and almost everything works fine. Except the source code viewing of the imported library. I can't figure out how to link my existing sources in Eclipse. I know about the "attach source" button. But if I click it and select the correct source folder, Eclipse tells me that the folder doesn't contain any source files.
It seems to me that I do something wrong, but I just can't figure out, what it would be.
Ok, I needed to search just a little bit more. With the help from this post ( First answere and second comment of that answere, all by #Ed Staub ), I figured it out.
The easiest way is to generate a new jar, but this time with java files. Name it and copy it to the lib folder. Use the "attach source" button again and navigate to the lib folder and select the source.jar.
Now, everything should work the right way.

How should resources in a compiled jar be accessed?

First of all, I have read through many S.O. questions regarding this topic and I have tried the things suggested in them.
Here is my situation. I am writing a Java app using the Processing framework and I'm in the final stages where I need to begin thinking about packaging the app. A jar file that is executable from the command line is what I'm attempting to build using the Export feature in Eclipse.
The structure of my project looks like this:
src/
multiple packages/
libs/
jar files and natives
data/
fonts and images
config/
json files
When I export the jar file and uzip the jar to inspect it's contents, I find that the contents of these dirs have been dumped in the top level of the .jar.
Which looks like this:
.jar
packages
jar files
fonts
json files
So, when I attempt to load a config file with something like:
BufferedReader reader = new BufferedReader( new FileReader( path ) );
Everything works just file when I run the app in Eclipse. But the jar file throws a FileNotFoundException.
Many of the questions that I've seen on S.O. regarding problems like these recommend using using class.getClass().getResource() or class.getResourceAsStream(). I have tried both of these using relative paths and just the file name as in:
class.getResource( 'config.json' );
class.getResources( 'cfg/config.json' );
class.getResourceAsStream( '../../config.json' );
All of these methods return null, when run from either Eclipse or the jar using:
java -jar myjarfile.jar
I am also open to using an Ant file. In fact, I'm now using the Ant file generated by the export feature to build the jar. If there is something I can add to that to add the directories into the jar that would be great too.
To reach resources in the root, prepend a / to the path. If not, you do it relative to the current package, which is usually only useful if the resource is physically next to the class in your sources too.
So use class.getResourceAsStream("/config.json"); to reach config.json in the root of the jar.
Also note that jars-inside-jars are not directly supported.
Your .jar file should just include the directories related to the "package" for the compiled code. You might be referencing a .war structure with /lib /WEB-INF etc. and this is different.
If your package structure is:
com.yourco.authentication
And your class in Login
Then your jar should be
/com/
/yourco/
/authentication/
Login.class
you then need the .jar in your classpath for the env to run via command line.
I see you note it works in Eclipse which likely has environment settings and imported required libs, etc. so hard to tell exactly. If your packages/ folder includes the compiled java code, I'm unsure if that'll work when referenced externally, thus suggesting you start your packages in the root folder.

making a .jar file with source and project structure?

I wonder if this is possible with ant and java:
I have a Java project which looks like this:
./
build.xml
src/
com/
example/
{.java files here}
bin/
com/
example/
{compiled .class files here}
lib/
{3rd-party jar files here}
dist/
{jar file(s) here}
I know how to make a .jar file that contains the stuff in the bin/ directory, with the right manifest to make it run my main Java class.
What I would like to do, if possible, is to make a .jar file that:
Java can execute ("java -jar myproject.jar")
Someone else can unzip to create the project structure. (including all the subdirectories except for the "dist/" directory)
Is this possible? I can't seem to tell Java to use the bin/ subdirectory of the .jar file as the classpath, I may be screwing up the syntax somehow.
edit: Okay, it sounds like trying to make one object serve two (too many) purposes. I have abandoned this approach, instead creating a standard .jar file as the executable, and a .zip file with the source (the build.xml + the src and lib directories). That way there are 2 easy downloads, more than 1 file but not too bad.
No, UrlClassLoader always tries to find classes based in the root, and a jar URL will always fetch entries based in the root of the jar file.
You could create a jar file which has binaries from the root, but source files under src etc. That wouldn't be too bad, assuming you really do just have com as the only "root" package.
You could put your project on a public SVN server, and just include instructions on how to check out the source - you'd also benefit from other people's check-ins and improvements (hopefully not vandalism).

Categories

Resources