I'm trying to build my first Pig UDF in Java and am having trouble calling the function when building with Eclipse (I have the pig 0.10.0 jar file in my class path). The source file is in /com/foo/bar/pig/IsInternal.java and the class file is placed in /bin/com/foo/bar/pig/IsInternal.class by Eclipse.
My code looks like this:
package com.foo.bar.pig;
// ... imports ...
public class IsInternal extends FilterFunc {
public Boolean exec(Tuple input) throws IOException {
// ... code here ...
return true; // or false
}
}
After I compile it, I run jar -cf PiggyBank.jar BiggyBank from just outside the project directory to package it all up into a JAR. When I run Pig, I try the following in the grunt shell:
REGISTER /full/path/to/PiggyBank.jar
DEFINE isInternal com.foo.bar.pig.IsInternal();
A = LOAD '/some/file/in/hdfs' USING PigStorage();
B = FILTER A BY isInternal($1);
At that point I get the following error:
ERROR 1070: Could not resolve com.foo.bar.pig.IsInternal using imports: [, org.apache.pig.builtin, org.apache.pig.impl.builtin.]
The Java code itself is fine (I've tested it), and I've tried playing around with different class paths in the DEFINE without any luck. I haven't found anything of help online. How would I fix this?
I'm a little queasy about the packaging into a .jar by hand. Can you try putting into an eclipse project and exporting from there?
Also, can you confirm the directory structure of the jar? It should be like this: PiggyBank.jar/com/foo/bar/pig/IsInternal.class
This error look like you not fix the class path path error for your UDF i was also have the same problem you in this link i explain how to fix above error . Hope it will help you .
Related
I am currently trying to include the jnativehook library and test out one of its examples.
I downloaded the .jar file from its website and created a .java file which has an example program. These are in the same folder.
I have followed other questions on here which have tried to tell me how to compile and run the program. I have tried:
javac -classpath jnativehook-2.1.0.jar GlobalKeyListenerExample.java
java -classpath jnativehook-2.1.0.jar GlobalKeyListenerExample
It compiles fine, however when I enter the second command to run it I get:
Error: Could not find or load main class GlobalKeyListenerExample
Caused by: java.lang.ClassNotFoundException: GlobalKeyListenerExample
All I want to do is be able to include a .jar file in my project and compile it from the command line. I am not using eclipse at the moment because I am getting too many problems with it and I also want to learn to program without IDEs.
(I am on windows by the way). Also, please do not mark this as duplicate. If it was actually a duplicate then I would be able to find a solution to my problem from the other similar questions.
Following the documentation of javac, especially "Example 3 - Specify a User Class Path" (at the bottom of the page), your java file has to have a package:
package com.example;
public class GlobalKeyListenerExample {
public static void main(String ... args) {
}
}
Then that file has to lie in the corresponding directory structure:
..something/com/example/GlobalKeyListenerExample.java
Now you can call the compiler with
javac -classpath jnativehook-2.1.0.jar;...something com/example/GlobalKeyListenerExample.java
And after compiling you can start it with
java -classpath jnativehook-2.1.0.jar;...something com.example.GlobalKeyListenerExample
I saw this link which uses Instrumentation to calculate the size of objects during runtime. I decided to try this library since It can be really helpful in determining the size of big data structures.
So I wrote the following code in a new project named TrySizeOf using NetBeans IDE:
package trysizeof;
import net.sourceforge.sizeof.SizeOf;
public class TrySizeOf {
public static void main(String[] args) {
String s = "abc";
System.out.println(SizeOf.deepSizeOf(s));
}
}
After that I created a folder named lib inside my project, and placed SizeOf.jar in it. Then under Project->Properties->Run I placed the following parameter:
-javaagent:/home/MyUserName/NetBeansProjects/TrySizeOf/lib/SizeOf.jar
However, when I attempt to run my project I get the following error:
Error occurred during initialization of VM
Error opening zip file or JAR manifest missing : /home/MyUserName/NetBeansProjects/TrySizeOf/lib/SizeOf.jar
agent library failed to init: instrument
Can any one help me with this? or maybe explain what did I do wrong?
UPDATE:
I found the error, it was a problem with the upper and lower cases. When I started the project for the first time I made a little upper and lower case typo. When I paid attention to it I fixed it, but I kept getting the same error (However the path was fixed). Now, I tried using clean and build and the project worked. It was that when I do a clean and build manifest file gets updated which is used to specify the path of premain method. When I tried clean and build the file got updated and the project finally worked. Thanks for the comment about a typo in my post, which somehow made me think about this.
In Eclipse, I wrote a Java class Test with a main() function.
The project in which is defined the class, I added the jar file bcprov-jdk15on-151.jar (I am using the library BouncyCastle).
In Eclipse, there is no problem and my program runs normally. But when I try to do it in a terminal, I get an exception.
After checking SO I found a similar post: NoClassDefFoundError while running a java program in terminal from IDE but the solution given doesn't work.
To illustrate my case, in the directory C:\Docs\workspace\Terminal\bin\ I have the file Test.class. If I run java Test I get Exception in thread "main" java.lang.NoClassDefFoundError: org.bouncycastle.math.ec.ECFieldElement.
If I run java -cp bcprov-jdk15on-151.jar Test (I put the .jar in the same directory to simplify) I get Error: Could not find or load main class Test so it seems that the dependency error is solved but another one occurs.
What am I doing wrong? Just to give the structure of my .java file:
import java.io.*;
...
public class Test {
... local methods ...
public static void main(String[] args) {
...
}
}
Thanks in advance.
Try this, you forgot to include current path "."
java -cp ".;bcprov-jdk15on-151.jar" Test
Hope it help
When trying to export a project as a .jar file, Eclipse will complain that some files can't be found on the classpath:
Class files on classpath not found or not accessible for: 'proj/src/main/java/analysis/specification/VerifyVariableIsDefinedInsideTargetTerm.java'
Now, the interesting part is that these files haven't actually existed on the disk for ages. I guess I force Eclipse to refresh its database some way. I've tried doing F5 for a full build, but it seems to no avail. I've also tried the procedure described in this blog post, but the issue remains.
Error… I actually tripped on the reason for the problem a few moments ago…
All the files that were raising problems did actually exist on the project but were commented out (and thus, I didn't see them in the package explorer which is the view I generally use).
After deleting them, everything went fine. I still don't understand why would they raise errors in the JAR creation process, though…
EDIT:
Here's an example of a commented out class:
package a.b.c
//import java.io.Closeable;
//public class Example implements Closeable {
// public void close() throws IOException {
// System.out.println("closed");
// }
//}
Because the whole class is commented out, no Example.class is created. Whereas if the class is commented out like this:
package a.b.c
//import java.io.Closeable;
public class Example
// implements Closeable
{
// public void close() throws IOException {
// System.out.println("closed");
// }
}
an Example.class is created, although empty.
This may be a bug in eclipse. If you export the ant build script from eclipse and use that, it creates the JAR just fine.
This issue occurred to me also. However, the problem that I had was that I had JRE configuration issues in Eclipse.
Probably the real problem is that not all your class files are in the same place… My suggestion is to move all your .java files into the same folder, recompile to make the .class files, make sure all the .class files are then in the same folder… Then, check out this webpage, if you need any help on actually creating the .jar.
The website also assumes you are on a Mac, but you should still be able to use a command prompt on Windows to get it work.
I have just created my first JAR in Eclipse, just a simple program with a single class Database.class. It is not in a package.
public class Database {
public Database() {
int dbInit = 1; } }
I have added it as an external JAR to the build path libraries for another project in Eclipse, but for some reason I cannot get Database db = new Database(), the default constructor, to work - it's as if the contents of the JAR are not being recognised.
Could anyone please offer any advice on this?
Thanks very much,
M
typically this works, so relax: you did some mistake and can fix it.
check content of your jar: run
jar vft myjar.jar
You should get output like
Database.class
Check that it is exactly what you get. Your class file must be at the root of the jar.
Verify that you are adding it to your second project correctly: Project/Properties/Java Build Path/Libraries, push button "Add external jars...", navigate to the jar and add it.
Now try to write in any java class of your project: Datab then push ctrl/space
It should complete to Database. Continue coding and enjoy.
BTW: why did you put your class to default package? I'd suggest you to put it into package. It will help you to avoid mistakes. for example probably you have other class named database in your code. How are you planning to resolve this conflict?
You might have to rebuild or clean your project.
The build path is not used at runtime. In your run configuration there's a tab to allow you to specify the classpath used when running the app.