I have three source files in a folder. I simply want to compile them using commandline and then execute them. However, I'm having the following issue. I am on a windows box:
Code compiles fine:
C:\mycode\src\code>javac Source1.java Source2.java Source3.java
Does not run from the folder where class files are:
C:\mycode\src\deckofcards>java Source1
Exception in thread "main" java.lang.NoClassDefFoundError: Source1 (wrong name: code/Source1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Source1. Program will exit.
However, it does run when moving to parent folder:
C:\mycode\src\code>cd ..
C:\mycode\src>java code/Source1
Hello1
Hello2
Hello3...
......
....
I want to distribute this source to someone and I'm not sure whether they will be on windows or unix box.
Eventually I want to create an executable jar file but that also was giving me an error like this.
Problem here is not with running Java files, it runs fine. So you wanted to understand the following:
In order to run a Java program you need to do the following steps:
Create Java source files (*.java files)
Compile using javac to binary class files (*.class files)
Run the class files using java executable
We use package structure to keep organize the java files. In your case it is a simple application (and you use the package named "code"), but in a large production application we need to organize our code into many packages.
While running the class file, we need to give the class file path (which includes the package path). So in your case you need to execute java code/Source1 to run your class.
You can learn more from this link: http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
For creating a JAR file you can refer the link: http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
For making a JAR file executable ensure you add manifest file with main-class attribute.
Related
I asked a question earlier here: NoClassDefFoundError with bluecove's javax.microedition
but got no responses.
I am trying to run some java code (Server.java) that requires bluetooth, so I installed the bluecove-2.1.0.jar file in C:\Program Files\Java.
I compile my code with javac -cp "C:\Program Files\Java\bluecove-2.1.0.jar";. Server.java and run it with java Server -cp "C:\Program Files\Java\bluecove-2.1.0.jar"
It compiles successfully, but when I run it, I get the error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: javax/microedition/io/Connection
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.microedition.io.Connection
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
Any ideas where i've gone wrong?
EDIT: Following Minus' answer, to fix the error that followed, I also needed to use a different .jar file which I downloaded from http://snapshot.bluecove.org/distribution/download/2.1.1-SNAPSHOT/2.1.1-SNAPSHOT.63/
The problem is that the .dll is not loaded correctly.
The library should contain a .dll that will be saved on disk and loaded, maybe the program cannot save the .dll to disk or cannot read it.
Extract the .dll and follow bluecove installation instructions on Native Library location.
For example:
Create a directory c:\blue, and a directory blue\lib.
Put the files like this:
C:\blue\Sever.java
C:\blue\Sever.class
C:\blue\[bluecove_native_library].dll
C:\blue\lib\bluecove-2.1.0.jar
Open a cmd (Command Prompt) on c:\blue
Try to run the app like this:
java -cp .;lib\bluecove-2.1.0.jar -Dbluecove.native.resource=false Server
This way you will have both Server.class and bluecove-2.1.0.jar in classpath and the DLL in path (since on Windows the current dir is in path).
Add a current folder to a classpath (.;) and make sure that your code is in a package-name folder.
So if your server has a package name :
mypackage
make sure that your class code is in a /mypackage directory
than go to a parent folder to /mypackage and call:
java -cp ".;C:\Program Files\Java\bluecove-2.1.0.jar" mypackage.Server
i've got a big problem exporting my project as a jar file (inside my project i use weka). I'm using Eclipse.
I've added in Java Build Path weka.jar and i've attached to it weka-src.jar, testing my classifier under eclipse everything works, so i've decided to create a jar (because i need that another program use it).
First of all i've created the manifest file:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ weka.jar weka-src.jar
Class-Path: .
Rsrc-Main-Class: start.Home
Main-Class: start.Home
The main class of the program is Home (inside the package start), while weka.jar and weka-src.jar are inside the /src/ directory of the eclipse program.
When i try to execute the jar created i receive this output:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: weka/classifiers/Classifier
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: weka.classifiers.Classifier
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
I think that the problem is the CLASSPATH so i've tried to solve it in this way:
I've created the Environment Variable CLASSPATH with this values:
C:\Users\tomma\Desktop\workspace Eclipse\AndroidPermissionsClassifier\src\weka.jar
C:\Users\tomma\Desktop\workspace Eclipse\AndroidPermissionsClassifier\src\weka-src.jar
but nothing changed, can someone help me please?
You are right: the classpath is the problem. Did you remove the items from the manifest classpath when you added them to the environment CLASSPATH? (Side note: place the source jar in the -sourcepath, not the classpath.)
I have a java application where main class is dependent with some other classes. I can run the application using an IDE well. But when I want to run the application from command line by locating to the main class's class file using java mainClass, it gives me the following error [the main class name is mainClass]:
Exception in thread "main" java.lang.NoClassDefFoundError: mainClass(wrong name: mainfolder/mainClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Can anybody suggest me how can I run my application without using any IDE?
Uou need to indicate the fully qualified name. For example if MainClass is under package com.example, you should execute java com.example.MainClass
You can check more here.
From documentation:
By default, the first non-option argument is the name of the class to
be invoked. A fully-qualified class name should be used. If the -jar
option is specified, the first non-option argume nt is the name of a
JAR archive containing class and resource f iles for the application,
with the startup class indicated by the Main-Class manifest header.
The Java runtime searches for the startup class, and other classes
used, in three sets of locations: the bootstrap class path, the
installed extensions, and the user class path.
Try mainfolder.mainclass instead of mainfolder/Mainclass. Also, the mainfolder is the package.
Make sure you put all needed jars in the classpath with -cp.
With or without IDEs, java application can always start by
java -cp classpath EntryClass
If you run the application with IDEs, the IDE you using prepares the classpath for you. If you run without the IDEs, you should prepare the whole classpath yourself.
The EntryClass is also found through the classpath.
Please refer to java - the Java application launcher for details.
Let say you are runngin your java program from /home/flyleaf/app/
This would be the command you need:
javac -classpath /home/flyleaf/app/jar1.jar:/home/flyleaf/app/jar1.jar:/home/flyleaf/app/ com/app/your/mail/class/Main.java
You need to add to the classpath the jar files you need and the path where you got your program
Ok! Got the output after executing the java command over the main class's class file with package.classname from outside the folder that contains the class files!
Besides the jar file is also working after locating the jar file and by executing this command java -jar jarname.jar!
Thanks everybody!
I'm trying to start my java game but I have some troubles with the java command line:
Here is what I type:
C:\>java -Djava.library.path=%cd%\lib -jar game.jar
And here is what I got:
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
at com.game.Main.main(Main.java:7)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
I can start my game with Eclipse (With Run as Java Application) but after the menu I got the following error:
Exception in thread "Thread-5" java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:72)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:65)
at org.lwjgl.Sys.loadLibrary(Sys.java:81)
at org.lwjgl.Sys.<clinit>(Sys.java:98)
at org.lwjgl.opengl.Display.<clinit>(Display.java:129)
at com.game.displaygui.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
lwjgl.jar has been put into the folder \lib.
Could you explain me with I got that?
Thanks.
This because lwjgl library is made by two components:
the .jar file which contains Java code
and the native binary library (which can be .so or .dll or .dylib according to your OS)
The first error you are getting is because you are setting the library path, that should contain the native library, but it does contain the .jar. So you get a java.lang.NoClassDefFoundError because you should set either the library path to the folder that contains native library, either the classpath to contain the real lwjgl.jar file.
The second error that you get with Eclipse is a successive step: your classpath contains the jar library but it is not able to find the native library attached to it, you can fix it in the following way:
You should specify explicitly which lib files to include (seperated by ;) :
-cp %cd%\lib\lwjdl.jar;%cd%\lib\<another-lib>.jar
Don't be tempted to use the wildcard * as it will cause more harm than good (from previous experience :))
You use the java.library.path option to specify the location of native libraries to load. In the location pointed to by that option you would place all dll or .so files required by LWJGL. On the other hand, you need to make sure that all required JAR files are on your classpath, via the -classpath option. Currently, you have your JAR files in the wrong directory.
In Eclipse, if you using maven.
Add the following to the project "Run" : "VM options" .
-Djava.library.path=yourpath/youproject/java/target/natives
I have compiled a java project into a jar but I am having some issues when trying to run it
java -jar XMltoCSV.jar
Error: Could not find or load main class XMLtoCSV.class
I have created a manifest file that contains:
Main-Class: xmltocsv.XMLtoCSV
(there is a newline character in the manifest text file)
The XMLtoCSV.class does contain the main() method. I have been running this code to create the jar file:
jar cvf XMLtoCSV.jar Manifest.txt xmltocsv/*.class
And I know its working because I get:
jar cvfm XMLtoCSV.jar manifest.txt XMLtoCSV.class
added manifest
adding: XMLtoCSV.class(in = 4885) (out= 2492)(deflated 48%)
Why does it give me this error? Thanks in advance for the help
It seems I am also running into another error, what does it mean?:
Exception in thread "main" java.lang.UnsupportedClassVersionError: xmltocsv/XMLt
oCSV : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: xmltocsv.XMLtoCSV. Program will exit.
Main-Class: XMLtoCSV.class
it's not right.
You have to specify something like:
Main-Class: com.mydomain.XMLtoCSV
namely the fully qualified name of the class.
See: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Drop the .class in your manifest.
Update:
What is the package of your main class? If your Java-File contains a statement such as
package com.google.common.base;
at the top. You need that package in your manifest. In your case that would be:
Main-Class: com.google.common.base.XMLtoCSV
If your class does not have such a package statemnent the error is something else.
(Note that com.google.common.baseis just an example of course.)
For a jar be executable, you have to be concerned with two thing on the manifest file:
1- The 'Main-Class' entry must be pointing to the class that'll be launching the application, i.e. the class where you declared the main method to start your app.
2- After all entries on the manifest file, you have to leave three lines in blank (I never found a documentation explaining why that is required but always works for me).
The simplest MANIFEST file would look like the following:
Manifest-Version: 1.0
Main-Class: com.foo.FOO
Just remember to leave three lines in blank after the last entry.
Hope I have helped.