I'm working on a JNI application. It is a C program that calls some Java methods.
I wrote it following some internet examples.
First I created a simple example in Eclipse Indigo (it created the Java files in a "default package"). Then I used something like this in the C code:
options.optionString = "-Djava.class.path=/home/elias/workspace/Funciones/bin";
All worked fine but then I re-made the Java proyect to have the Java code in a package called "Funciones", so I modified to:
options.optionString = "-Djava.class.path=/home/elias/workspace/Funciones/bin/Funciones";
But now I doesn't work... I supposed it is something I have wrong in the classpath.
Can someone help me please?
Thanks.
You want the class path to point at the directory (or directories, or jar files) holding the top-level packages you're using, not inside the packages. So if your code is laid out like this:
~/Funciones/bin/
|
>----Funciones
| |
| >----Funciones.class – this is the class Funciones.Funciones
|
>----some other package
|
:
you need to add ~/Funciones/bin to your classpath. To find the class, you need to use its fully qualified name – the class name prefixed with the package name:
Class clsFunciones = Class.forName("Funciones.Funciones");
or, in JNI, the class descriptor:
jclass clsFunciones = (*env)->FindClass(env, "Funciones/Funciones");
Related
I'm revisiting Java after briefly learning it in high school to work through the lwjgl git book. There's a section that kind of loses me where the fragment shader / vertex shader is supposed to be loaded, using the function 'getResourceAsStream'.
I've tried different approaches, and vainly tried to debug / print the locations where the JVM was searching for the file, but I'm kind of at a loss. I am very confused, but would also prefer understanding why this code doesn't work on my machine, and how to get it to work (with this package / class loading approach).
Since the classes both share the 'main' class, I thought the JVM would search starting there for a 'resources' folder. But this does not seem to be the case.
I do think it has to do with the package declarations, but I don't understand enough to proceed.
Here are the relevant files and locations... I tried to only include what I thought important.
// project-root/src/main/java/org/lwjglb/engine/Utils.java
package main.java.org.lwjglb.engine;
// other imports...
public class Utils {
public static String loadResource(String fileName) {
InputStream in = class.forName(Utils.class.getName()).getResourceAsStream(fileName);
// 'in' is null after this call
// other code...
}
}
// project-root/src/main/java/org/lwjglb/game/Renderer.java
import main.java.org.lwjgl.engine.Utils;
// other imports...
public class Renderer {
public void init() {
Utils.loadResource("/vertex.vs"); // fails
}
}
// project-root/src/main/resources/vertex.vs
// vertex shader code...
The issue is that the getResourceAsStream() call returns null, so the following code that depends on a valid result crashes with a java.lang.NullPointerException.
What path should I pass in to getResourceAsStream() so that it will not return null?
Assume the following directory structure:
|-- src
`-- test
|-- java
| `-- shaders
| `-- ShaderParser.java
`-- resources
`-- shaders
`-- fragmentShader.txt
The proper way to get the resource "fragmentShader.txt" from "ShaderParser.java" is:
Class clazz = Class.forName(TestUtils.class.getName());
InputStream in = clazz.getResourceAsStream("../../resources/shaders/fragmentShader.txt");
The ../ means "traverse upward one directory".
The path starts in the src/test/java/shaders/ directory so we use ../../ to traverse upward twice into src/test/ and then tack on the rest of our path resources/shaders/fragmentShader.txt.
It is important to remember that when using Class#getResourceAsStream(), it uses the package that the class resides in as the base directory. It ONLY uses relative paths because it is trying to load the resource using that class' ClassLoader. and will not work if supplied with a fully qualified path or a path starting at the project root.
Please refer to the documentation for more information.
I had erroneusly thought that Java had a reserved directory (e.g. 'resources') where it would look first to load files. Kind of similar to Flask in python.
However, I understand now that the class.getResourceAsStream() function either takes relative paths (to the current 'lowest' level package) or absolute paths (from the project's top-level root).
I had furthermore erroneously thought that I could try doing a relative path load, from one of the higher-level packages. As in, I'm in package main.java.org.lwjgl.engine.Utils, but when looking for the file main.resources.vertex.vs, I could start the search from the 'main' package and pass 'resources/vertex.vs' or 'resources/vertex.vs'. I don't know if this is possible, but it feels erroneous.
Instead, the solution is to pass the file location '/main/resources/vertex.vs'. This will start at the project-root, and correctly load the file.
I'm still confused as to how the lwjgl code worked, but I see that it could be possible that the classPath was set to 'main', so passing an absolute path could work.
I have a jar file that contains the following:
LibJar Contents
dir1
|dir1-1
| |Class1-1-1
| |LClass1-1-2
|Ldir1-2
|LClass1-2-1
Ldir2
|LClass2-1
My java program (we can call it ProgJar, but I also run it in Netbeans IDE) has the following package structure:
ProgJar
dir1
|dir1-1
| |Class-1-1
| |PClass1-1-2 Different file name from LibJar
Pdir2
|PClass2-1
The only shared package structure between ProgJar and LibJar is "dir1/dir1-1/Class1-1-1". Everything else prefixed with a P is unique to ProgJar and everything prefixed with a L is unique to LibJar.
I use LibJar as a library in ProgJar.
This is the snippet of code I run in ProjJar:
ClassLoader clP = Pdir2.PClass2-1.class.getClassLoader();
ClassLoader clL = Ldir2.LClass2-1.class.getClassLoader();
URL u1 = clP.getResource("dir1/dir1-1");
URL u2 = clL.getResource("dir1/dir1-1");
System.out.printf(u1.toExternalForm());
System.out.printf(u2.toExternalForm());
When I run this in Netbeans I get the following output:
Netbeans Output:
jar:file:/C:/path/to/project/lib/LibJar.jar!/dir1/dir1-1
jar:file:/C:/path/to/project/lib/LibJar.jar!/dir1/dir1-1
When I run as a ProgJar as a built jar outside of netbeans, I get:
Jar Output:
jar:file:/C:/path/to/ProgJar/ProgJar.jar!/dir1/dir1-1
jar:file:/C:/path/to/ProgJar/ProgJar.jar!/dir1/dir1-1
What I expect to see is the following:
Netbeans Output:
jar:file:/C:/path/to/project/build/classes/dir1/dir1-1
jar:file:/C:/path/to/project/lib/LibJar.jar!/dir1/dir1-1
Jar Output:
jar:file:/C:/path/to/ProgJar/ProgJar.jar!/dir1/dir1-1
jar:file:/C:/path/to/ProgJar/libs/LibJar.jar!/dir1/dir1-1
I read through a few different articles, but this one seems somewhat relevant to this particular issue:
http://jeewanthad.blogspot.com/2014/02/how-to-solve-java-classpath-hell-with.html
How am I able to achieve my specified output?
Below code is not doing what you are expecting it to do:
ClassLoader clP = Pdir2.PClass2-1.class.getClassLoader();
ClassLoader clL = Ldir2.LClass2-1.class.getClassLoader();
Here clP amd clL are same Classloader instances(you system/application classloader to be specific).To verify, just see (clP == clL) should return true.
What you want to do is, use a custom classloader(URLClassLoader should do) to load your library. Then, the system classloader that loaded your ProgJar and your custom classloader will be different. Then rest of your code should work as expected.
I wrote the following piece of code in sublime text on my macbook pro.
I saved the file inside the java folder on my desktop. When I tried to compile the program and tried to execute it, I am getting the fallowing error message " Error: Could not find or load main class Animals ".
Could someone help me in compiling and running this program
package forest;
class Animals{
public static void main(String[] args)
{
Animals s = new Animals();
Sring[] s2 = s.getAllAnimals();
}
public String[] getAllAnimals()
{
String[] s1= {"Lion", "Elephant", "Tiger","Deer","Wolf","Dinosar"};
return s1;
}
}
Make your class Animal as public and rename the file to Animal.java
Your class is in a package called forest so you need to move Animals.java into a directory called forest.
You hava a typo on line 7: Sring[] s2 = should be String[] s2 =.
Compile from the parent directory of forest. Something like this should work
$ javac forest/Animals.java
Run from the parent dir:
$ java forest.Aminals
Your program will have no output when you run it.
Look on your code there is no sring type class in java so change it to String
Sring[] s2 = s.getAllAnimals();
change it to
String[] s2 = s.getAllAnimals();
Everything will be good
try this tutorial Java and the Mac OS X Terminal and Make your class public and also make sure that the file name that contains your source code and the name of your main class is same, your main class is the one that contains main method.
First of all you can name your java file with any name.
Say Sample.java. and let us consider this file is present in D:\work
Go to D:\work folder in cmd prompt.
Keep this in mind :
If you write package forest, then your class has to be public.
So write public class Animals{}
While compiling do this javac -d Sample.java
By doing this the compiler will create the folder "forest" and inside that it will create the Animals.class.
So now in D:\work we have Sample.java file and "forest" folder
Now in your command prompt DO NOT go inside the "forest" folder.
Stay in work folder. D:\work
And Run this command from D:\work java forest.Animals
Explantion::
The name of your class that contains Main method is not Animals. It is forest.Animals This is the fully qualified name of the class. This happened becasue you put a package to it.
So while runnning you must run with fully qualified name
java forest.Animals
The name of your class is not Sample.java. <-- This is just a text file name it anything the complier will not generate a class with name Sample.class because inside this file there is no such class, the class name is Animals. So Animals.class will be generated.
And javac -d Sample.java helps you create the folder structure automatically based on the package.
For example if your class was like this
package com.stackoverflow.samples
public class Animals{
}
And you do java -d Sample.java
The Compiler will create a folder com inside that another folder stackoverflow inside that another folder samples and inside that a file Animals.class
To run this you must run it from outside the the "com" folder with fully qualified name
java com.stackoverflow.samples.Animals
I have the following question. Given a correctly compiled class whose source code is:
1. package com.sun.sjcp;
2.
3. public class Commander {
4. public static void main(String[] args) {
5. // more code here
6. }
7. }
Assume that the class file is located in /foo/com/sun/sjcp/, the current directory is /foo/, and that the classpath contains "." (current directory). Which command line correctly runs Commander?
A. java Commander
B. java com.sun.sjcp.Commander
C. java com/sun/sjcp/Commander
D. java -cp com.sun.sjcp Commander
E. java -cp com/sun/sjcp Commander
Answer: B
A. We are in the root dir, so we can't see the file from
/foo/com/sun/sjcp/ directly
B. is correct
C. I think is correct too ???
D. We are in /foo/com/sun/sjcp/ and there is only class file with
package package com.sun.sjcp; so the compiler can't find it.
If the file was without package declaration and was build in this dir
then it will work if we try to run it in this way.
E. The same as D - does not work
The only answer given is B. Where am I making a mistake?
C is incorrect because "com/sun/sjcp/Commander" isn't a valid class name. Path to a file and class name are not the same things.
(edited): It's appeared "that the jvm allows you to use a forward slash in place of a dot in the fully qualified name of the class to run" (but you must use only forward slashes). So it seems that C is also correct
A class name can never have forward slashes on it (it's not the same concept as a filesystem path), that's why B is the only right answer.
I am new to java and to the eclipse IDE.
I am running Eclipse
Eclipse SDK
Version: 3.7.1
Build id: M20110909-1335
On a windows Vista machine.
I am trying to learn from the book Thinking in Java vol4.
The author uses his own packages to reduce typing. However the author did not use Eclipse and this is where the problem commes in..
This is an example of the code in the book.
import java.util.*;
import static net.mindview.util.print.*;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
print("this does not work");
}
this is the contents of print.Java
//: net/mindview/util/Print.java
// Print methods that can be used without
// qualifiers, using Java SE5 static imports:
package net.mindview.util;
import java.io.*;
public class Print {
// Print with a newline:
public static void print(Object obj) {
System.out.println(obj);
}
// Print a newline by itself:
public static void print() {
System.out.println();
}
// Print with no line break:
public static void printnb(Object obj) {
System.out.print(obj);
}
// The new Java SE5 printf() (from C):
public static PrintStream
printf(String format, Object... args) {
return System.out.printf(format, args);
}
} ///:~
The error I get the most is in the statement.
Import static net.mindview.util.print.*;
On this staement the Eclipse IDE says it cannot resolve net
also on the
print("this does not work");
The Eclipse IDE says that the class print() does not exist for the class HelloWorld.
I have been trying to get these to work, but with only limited success, The autor uses another 32 of these packages through the rest of the book.
I have tried to add the directory to the classpath, but that seems to only work if you are using the JDK compiler. I have tried to add them as libraries and i have tried importing them into a package in a source file in the project. I have tried a few other things but cant remember them all now.
I have been able to make one of the files work, the print.java file I gave the listing for in this message. I did that by creating a new source folder then making a new package in that foldeer then importing the print.java file into the package.
But the next time I try the same thing it does not work for me.
What I need is a way to have eclipse load all these .java files at start up so when I need them for the exercises in the book they will be there and work for me, or just an easy way to make them work everytime.
I know I am not the only one that has had this problem I have seen other questions about it on google searches and they were also asking about the Thinking In Java book.
I have searched this site and others and am just not having any luck.
Any help with this or sugestions are welcome and very appreciated.
thank you
Ok I have tried to get this working as you said, I have started a new project and I removed the static from the import statement, I then created a new source folder, then I created a new package in the source folder. Then I imported the file system and selected the the net.mindview.util folder.
Now the immport statement no longer gives me an error. But the the print statement does, the only way to make the print statement work is to use its fully qualified name. Here is the code.
import net.mindview.util.*;
public class Hello2 {
public static void main(String[] args) {
Hello2 test = new Hello2();
System.out.println();
print("this dooes not work");
net.mindview.util.Print.print("this stinks");
}
}
The Error on the print statement is:
The method print(String) is undefined for the type Hello2
and if I try to run it the error I get is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method print(String) is undefined for the type Hello2
at Hello2.main(Hello2.java:6)
The Statement::::: net.mindview.util.Print.print("this stinks") is the fully qualified print statement and it does not throw an error but it does totally defeat the purpose of the print.java file..
If you have any questions please ask Ill get back to you as soon as I can.
I've had similar issues. I solved it by following the steps below:
Click File->New->Java Project. Fill in UtilBuild for the ProjectName. Chose the option "Use project folder as root and click 'Finish'.
Right-click on UtilBuild in the PackageExplorer window and click New->package. For the Package Name, fill in net.mindview.util
Navigate within the unzipped Thinking In Java (TIJ) folder to TIJ->net\mindview\util. Here you will find all the source code (.java) files for util.
Select all the files in the net\mindview\util folder and drag them to the net.mindview.util package under UtilBuild in Eclipse. Chose the 'Copy Files' option and hit 'OK'.
You will probably already have the 'Build Automatically' option checked. If not, go to Project and click 'Build Automatically'. This will create the .class files from the .java source files.
In Eclipse, right-click on the project you were working on (the one where you couldn't get that blasted print() method to work!) Click Properties and Java Build Path->Libraries. Click 'Add Class Folder...' check the box for UtilBuild (the default location for the .class files).
I think the confusion here arises due to CLASSPATH. If you use Eclipse to build and run your code then Eclipse manages your CLASSPATH. (You don't have to manually edit CLASSPATH in the 'Environment Variables' part of your computer properties, and doing so changes nothing as far as Eclipse Build and Run are concerned.)
In order to call code that exists outside your current project (I will name this 'outside code' for convenience) you need to satisfy three things:
A. You need to have the .class files for that code (as .class files or inside a JAR)
B. You need to indicate in your source code where to look for the 'outside code'
C. You need to indicate where to start looking for the 'outside code'
In order to satisfy these requirements, in this example we:
A. Build the project UtilBuild which creates the .class files we need.
B. Add the statement import static net.mindview.util.Print.*; in our code
C. Add the Class Folder library in Eclipse (Java Build Path->Libraries).
You can investigate the effect of Step C by examining the .classpath file that lives directly in your project folder. If you open it in notepad you will see a line similar to the following:
<classpathentry kind="lib" path="/UtilBuild>
You should combine this with your import statement to understand where the compiler will look for the .class file. Combining path="/UtilBuild" and import static net.mindview.util.Print.*; tells us that the compiler will look for the class file in:
UtilBuild/net/mindview/util
and that it will take every class that we built from the Print.java file (Print.*).
NOTE:
There is no problem with the keyword static in the statement
import static net.mindview.util.Print.*;
static here just means that you don't have to give specify the class name from Print.java, just the methods that you want to call. If we omit the keyword static from the import statement, then we would need to qualify that print() method with the class it belongs to:
import net.mindview.util.Print.*;
//...
Print.print("Hello");
which is slightly more verbose than what is achieved with the static import.
OPINION:
I think most people new to Java will use Eclipse at least initially. The Thinking in Java book seems to assume you will do things via command line (hence it's guidance to edit environment variables in order to update CLASSPATH). This combined with using the util folder code from very early in the book I think is a source of confusion to new learners of the language. I would love to see all the source code organised into an Eclipse project and available for download. Short of that, it would be a nice touch to include the .class files in just the 'net/mindview/util' folder so that things would be a little easier.
U should import package static net.mindview.util not static net.mindview.util.Print
and you should extend the class Print to use its method.......
You should remove the static keyword from your import decleration, this: import static net.mindview.util.print.*; becomes this: import net.mindview.util.print.*;
If that also does not work, I am assuming you did the following:
Create your own project;
Start copying code directly from the book.
The problem seems to be that this: package net.mindview.util; must match your folder structure in your src folder. So, if your src folder you create a new package and name it net.mindview.util and in it you place your Print class, you should be able to get it working.
For future reference, you should always make sure that your package decleration, which is at the top of your Java class, matches the package in which it resides.
EDIT:
I have seen your edit, and the problem seems to have a simple solution. You declare a static method named print(). In java, static methods are accessed through the use of ClassName.methodName(). This: print("this dooes not work"); will not work because you do not have a method named print which takes a string argument in your Hello2 class. In java, when you write something of the sort methodName(arg1...), the JVM will look for methods with that signature (method name + parameters) in the class in which you are making the call and any other classes that your calling class might extend.
However, as you correctly noted, this will work net.mindview.util.Print.print("this stinks");. This is because you are accessing the static method in the proper way, meaning ClassName.methodName();.
So in short, to solve your problem, you need to either:
Create a method named print which takes a string argument in your Hello2 class;
Call your print method like so: Print.print("this stinks");
Either of these two solutions should work for you.
In my case I've dowloaded and decompressed the file TIJ4Example-master.zip. in eclipse workspace folder. The three packages : net.mindview.atunit, net.mindview.simple and net.mindview.util are in this point of the project :
and java programs runs with no problems (on the right an example of /TIJ4Example/src/exercises/E07_CoinFlipping.java)