Problem in adding jar library in JNI - java

How to add a folder that contain jar files in jni c++. for example i have a folder "MyLib" that contains "Math.jar","Stats.jar" and "Temp.jar". I want to add "MyLib" to classpath insted of adding all the jar files like
options[0].optionString = "-Djava.class.path=c:\myjni\MyLib";
instead of
options[0].optionString = "-Djava.class.path=c:\myjni\MyLib\Math.jar ; c:\myjni\MyLib\Stats.jar ; \c:\myjni\MyLib\Temp.jar" ;

Well you cant do that. You are allowed only to add JAR files or directories with class files to the classpath.
You could extract all jars to a directory and add this directory to the classpath.
Actully there IS a trick. Java from version 6.0 by Sun adds a possibility to expand globs. This should work in Java 6+:
java -cp lib/*.jar
So you could try this:
options[0].optionString = "-Djava.class.path=c:\myjni\MyLib\*.jar";
I am not sure if it works with -D classpath setting. If you can provide the classpath cia -cp option do that. Please note the expansion is in the JVM has some limitations:
http://javahowto.blogspot.com/2006/07/jdk-6-supports-in-classpath-but-be.html

Related

How to import JAVA packages in cmd?

This is the first time I am compiling a program, and it doesn't seem to be working out. Looks like some packages are not being located - so for this question, I'll just focus on one:
Steps I've take so far:
1) setting up the System Variable Path to include java
2) in CMD.exe: jar tf log4j.jar I did this to make sure it includes log4j.Logger and it does.
3) I Shift+rightclick and open command prompt from this folder:
4) Then I enter javac TNT.java and i get the following error (along with others):
Any thoughts?
I set the classpath to the same folders with set classpath = "name of folder" no change...
edit
5) have also tried
javac -cp jdkbindirectory;jrebindirectory;theabovefolder TNT.java
I get this:
blahblahblah
You shouldn't set the classpath using an environment variable as it is bad practice. What if you accidentally change it later for a different project and your current project breaks?
When including classes in the classpath, you can include the path of the root of the package of the class, as in the folder that contains the folders in the package structure. However, when you're including a jar in your classpath, you need to put the entire path of the jar file (relative to the current working directory) all the way up to the jarname.jar.
Also, remember that by default, java looks in the current working directory and uses that as its default classpath. However, as soon as you specify a classpath it no longer does that automatically for you. Be sure that you're including your current directory in your classpath as well.
Finally, be sure to surround the classpath in quotes otherwise java might think its a part of another argument.
I would try this:
javac -cp "./;log4j.jar" TNT.java
And then to execute the class file:
java -cp "./;log4j.jar" TNT
Hope this works, good luck!

NoClassDefFoundError when runnable jar

Hi Guys I have included the Webcam-Capture API in my project.
When I run it in Netbeans everything works fine. But when i compile everything to a runnable jar i get this message trying to run it by cmd line.
can anyone of you help me?
i already tried to unbound and rebound all jars and changing jdks but its not working
add -classpath flag in the command line ,pointing to the path where Webcam-Capture API exists in your file system, unless you want to create a single package executable.In your case It should be something like below
java -classpath YOURJAR.jar;folder_of_dependant_jar/*;. com.awesome.pagackage.Starter
Where YOURJAR.jar contains the com.awesome.pagackage.Starter.main(String args[])
You also mentioned that your jar is a runnable jar it also means that while exporting/building you can do one of the following way.( NOTE , this feature is in eclipse , but you would get the idea ).Each of the following options you see in the library handling does specific things.
The first option: Extracts the dependent jar into your target jar as java packaging.This means if your package is com.awesome.package and the dependent jar has package logic.package; , after the runnable jar is build you could find both these package exists in your jar file.
The second option: I think it is more on eclipse specific since eclipse adds few classes of its own , of runnable generation, so I am not explaining it here.
The third option : is the most interesting one. it creates folder stucture like below
ndon_lib\external.jar ( external jar file )
ndon.jar ( your jar file )
This time the manifest.mf file contains something like below.
Class-Path: . ndon_lib/external.jar
Main-Class: com.awesome.pagackage.Starter
You should set the classpath
java -cp "your.jar" "yourclass"

Setting classpath for a jar and a class

I need to set the classpath for servlet-api.jar and another class in order to compile a file.java.
How could I accomplish that? I have tried
javac -cp /path/to/servlet-api;/home/user/Desktop/Other.class file.java
However it does not work.
Any help? I am aware it's possible to set the environment variable however I would like to know if it's possible manually. Thanks
The classpath must contain jar files, and directories. Directories must be directories containing the root of a package tree. So, assuming Other is in the package com.foo.bar, and its class file is /home/user/Desktop/com/foo/bar/Other.class, the classpath should be
-cp /path/to/servlet-api.jar:/home/user/Desktop
Note that : is the path separator on Unix. ; is for Windows. I assume you're not on Windows since your path is /home/... and not c:\home\...
PS: If Other is not in any package, then fix that. Classes should always be in a package.

SCons for Java; Is there an analog for env.Program()?

I need to create an executable file, run, that will call # java for my classes
I am compiling my java project with SCons:
libFiles = "lib/myLibs.jar"
# Build the environment
env = Environment(JAVACLASSPATH = libFiles, JAVASOURCEPATH = '.')
env.Java(target = 'classes', source = 'src')
All of the classes are stored in folder classes/ and all the source files are in /src . To run the program, I have to
# cd classes/
# java -cp . myProg
Is there a way to have SCons create an executable in the root directory so it can call java by itself? I looked at an existing project that used env.Program() but that was only for C++.
Thanks!
You may use the Jar builder. The following SCons example does what you want.
jar = java_env = Jar(target='Observer',
source=['Observer.java',
'Manifest.txt'])
Note that if you want the Manifest.txt file to work as well it must have the following first line:
Manifest-Version: 1.0
You should only use the Java builder if you want to generate the .class files.

Package not found; javac

This is annoying.
I have a directory structure like this
-lib
--some jar files
-packageName
--Main.java
--SomeOtherPackage
--SomeOtherJavaClass.java
Main.java imports SomeOtherPackage. And both java files uses jars in the lib.
What I do is add the jar files independently in the CLASSPATH. And then run as:
javac packageName/Main.java
but it gives the error that Package not found SomeOtherPackage . Shouldn't it automatically realize the dependency and build SomeOtherPackage as well? What would be the javac command and the classpath for the above case?
Thanks
The normal practice is to add the package root to the classpath.
When you're already in the package root, use -cp .. E.g.
cd /path/to/all/packages
javac -cp . packageName/Main.java
If you want to include JAR files as well, use the ; (or in *nix, the :) as classpath path separator:
javac -cp .;lib/file.jar packageName/Main.java
To save the time in repeating all the typing of shell commands, use a .bat (or in *nix a .sh) file. Or just an IDE if you're already familiar with java/javac and so on.
You need to add packageName to the CLASSPATH so it can find SomeOtherPackage

Categories

Resources