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
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'm just getting started with Java. This is my program:
package javaapplication1;
public class JavaApplication1
{
public static void main(String[] args)
{
System.out.println("Hello ");
}
}
If I remove the first line package javaapplication1, the code won't run. I did the same thing in class but it was working.
Can someone explain why does this happen?
If you are working with terminal along with package statement, save your code as JavaApplication1.java
and then compile your current code with below syntax.
javac -d . JavaApplication1.java
( -d . indicates create directory in current location because we are using package statement).
then for execution of your code you need to change directory with
cd javaapplication1
then execute your code with
java JavaApplication1
It will execute fine.
But if you are working without package statement seen will differ, you need to compile code normally with
javac JavaApplication1.java
then execute code with
java JavaApplication1
You will not get an error.
Note: But If you are using any IDE nothing to worry about. IDE will take care of package statement.
error: Could not find or load main class: this error appears at runtime if JVM is unable to find main class.
You need to change the directory as I mentioned above, then it might work fine.
This has to do with the way your IDE sets up your project.
When you start learning a new language, it's always a good idea to skip the IDE until it actually becomes useful. I suggest you copy your program to a basic text editor, remove the package line, save it as JavaApplication1.java and manually compile and run with javac JavaApplication1.java and java JavaApplication1.
I wrote and tested a small Java program using Eclipse. I'm now trying to deploy it on a Windows 7 box and Java cannot find the class. I copied the class file to C:\dxtester\classes. I'm trying to run it from the dxtester directory with: C:\dxtester>java -classpath classes;. dxtester
This produces this exception which I think I understand. Java examined the class file and is prompting me to provide the fully qualified name.
Exception in thread "main" java.lang.NoClassDefFoundError: dxtester (wrong name:
dxtester/dxtester)
If I use the FQN I get
C:\dxtester>java -classpath classes;. dxtester.dxtester
Error: Could not find or load main class dxtester.dxtester
The application is a simple test driver where everything is done in main().
package dxtester;
public class dxtester {
public static void main(String[] args) {
This seemed like an extremely simple thing to do but I'm completely baffled. What am I missing?
Your current directory is dxtester;
in this directory you have dxtester.class (I presume);
your classpath is the current directory.
This setup is wrong: your classpath must be the base directory such that package names correspond to its subdirectories. In your case you should cd to C:\ and repeat the command; ideally, however, you will have your package structure in a dedicated directory instead of the root.
I should also mention that class names should be in CamelCase.
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 .
package pack;
public class sample{
public static void main(String input[])
{
NumberFormat numberFormat = new DecimalFormat("#,##0.00##");
System.out.println(numberFormat.format(44533125.00));
}
}
the code is working fine in the current dir.. (c:/myprogram/).
after that i copy the sample.class file and paste it in other dir(d:/myprogram).
i got error while running, like
Exception in thread "main" java.lang.NoClassDefFoundError: sample (wrong name: pack/sample)
In java .class file can run anywhere right? but why i am not able to run?
You should have the class file within the package - so it should be in a directory called pack. Then with the parent directory in the classpath, you'd run
java pack.sample
(You should also change the class name to Sample to follow conventions, btw - and run pack.Sample.)
If you're building with javac, specify the "-d" option to tell it the base directory, and it will create the appropriate package structure if necessary. For example:
javac -d classes Sample.java
or
javac -d classes src/pack/Sample.java
will (in both cases) create
classes/pack/Sample.class
You could then run
java -cp classes pack.Sample
IntelliJ and maybe other IDE's do not refactor your Run/Debug configuration. You must manually change your package name preceding the name of your Main class. For instance, change 'sample.Main' to 'com.company.package.ui.Main' so it will launch correctly next time you try to run it.
The IDE might have already marked the Run/Debug button with a red cross because it couldn't find the main class. It also gives a warning when you open the Run/Debug configuration.
If you are not using a single java/class file you can also remove the package statement.