Additional jar files for java.exe -jar - java

I run the following command:
C:\Projects\java -cp ./dependency.jar -jar ./dist/main.jar
Exception in thread "main" java.lang.NoClassDefFoundError: MyException
Caused by: java.lang.ClassNotFoundException: MyException
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: Main. Program will exit.
MyException is inside dependency.jar. I also tried to replace -cp with -classpath. What do I do wrong?

The -cp options is ignored if you use -jar. If you need extra jar-files on the class-path, you should specify this in the Manifest of main.jar like this:
Class-Path: dependency.jar
(Then you can run your program using java -jar main.jar.)
Related questions:
When creating a jar file how do I retain the existing classpaths to other jar files?
Cannot find class even when jar file is in working directory
Jetty Classpath issues
problem related to MANIFEST.MF in jar

Have you tried specifying the class path in the windows environment variable CLASSPATH ?
specify your environment variable CLASSPATH and put the value of your JAVA_HOME\lib and (path to all your classes) and also include current directory by including (dot or period sign).

Related

Either java.lang.ClassNotFoundException or Could not find or load main class error

First time trying to run some Java code under Windows here, and I got:
D:\Tmp>java JDBC_SQLServer
Picked up _JAVA_OPTIONS: -Xmx512M
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
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 java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at JDBC_SQLServer.main(JDBC_SQLServer.java:19)
I.e., java finds main but didn't find the lib. Now, --
D:\Tmp>java -classpath "C:\Program Files\Java\jdk1.8.0_162\lib" JDBC_SQLServer
Picked up _JAVA_OPTIONS: -Xmx512M
Error: Could not find or load main class JDBC_SQLServer
I.e., java finds the sqlserver.jdbc.SQLServerDriver lib but can't find main now.
The JDBC_SQLServer is just a hack from
https://learn.microsoft.com/en-us/sql/connect/jdbc/connection-url-sample
How to solve it? Thx.
The classpath needs to contain the current directory (in order to find your class and its main-method and the jar-file (not only a directory with jars in it), so a call could look like this:
D:\Tmp>java -cp "C:/mssql/lib/sqljdbc41.jar;." JDBC_SQLServer
You don't need to use backslashes for the paths and because backslashes are often parsed specially by the shell you use, I personally try to avoid them as much as possible.

Issues when attempting to compile AspectJ tutorial application

I was trying to follow this tutorial:
http://andrewclement.blogspot.pt/2009/02/load-time-weaving-basics.html
but I stumbled across a couple of problems:
To start with, my system didn't have the javac and ajc PATH variables set, so I added the bin/ directory of my JDK7 to the PATH variable, as well as AspectJ's bin/ directory.
But when running ajc WhereDoesTheTimeGo.java Simple.java I got the following error:
[warning] couldn't find aspectjrt.jar on classpath, checked: C:\Program Files\Ja
va\jre7\lib\resources.jar;C:\Program Files\Java\jre7\lib\rt.jar;C:\Program Files
\Java\jre7\lib\sunrsasign.jar;C:\Program Files\Java\jre7\lib\jsse.jar;C:\Program
Files\Java\jre7\lib\jce.jar;C:\Program Files\Java\jre7\lib\charsets.jar;C:\Prog
ram Files\Java\jre7\lib\jfr.jar;C:\Program Files\Java\jre7\classes;C:\Program Fi
les\Java\jre7\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jre7\lib\ext\dn
sns.jar;C:\Program Files\Java\jre7\lib\ext\jaccess.jar;C:\Program Files\Java\jre
7\lib\ext\localedata.jar;C:\Program Files\Java\jre7\lib\ext\sunec.jar;C:\Program
Files\Java\jre7\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jre7\lib\ext\
sunmscapi.jar;C:\Program Files\Java\jre7\lib\ext\zipfs.jar;C:\Program Files\Java
\jre7\lib\tools.jar
[error] classpath error: unable to find org.aspectj.lang.JoinPoint (check that a
spectjrt.jar is in your classpath)
so I manually added the referred file to the classpath:
ajc -cp ajc -cp C:\aspectj1.7\lib\aspectjrt.jar Simple.java WhereDoesTheTimeGo.java
and then execution went apparently alright, being both .class files created.
But then...:
ajtests>java Simple
Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/Sign
ature
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.Signature
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)
... 6 more
so I once again added AspectJ's jars to the classpath:
java -cp C:\aspectj1.7\lib\aspectjrt.jar;C:\aspectj1.7\lib\aspectjtools.jar;C:\aspectj1.7\lib\aspectjweaver.jar;C:\aspectj1.7\lib\org.aspectj.matcher.jar Simple
But that will only yield
Error: Could not find or load main class Simple
What's going wrong here? Why is this giving me so much pain than what the tutorials would make me believe I'd have to have?
EDIT
I've noticed I only have to write
aj -cp .;;C:\aspectj1.7\lib\aspectjweaver.jar Simple
and that will work. What I'm finding strange is that aspectjweaver.jar seems to be already being passed on by the aj script:
#echo off
REM This file generated by AspectJ installer
REM Created on Tue Apr 16 19:02:36 BST 2013 by top_secret
if "%JAVA_HOME%" == "" set JAVA_HOME=C:\Program Files\Java\jre7
if "%ASPECTJ_HOME%" == "" set ASPECTJ_HOME=c:\aspectj1.7
if exist "%JAVA_HOME%\bin\java.exe" goto haveJava
if exist "%JAVA_HOME%\bin\java.bat" goto haveJava
if exist "%JAVA_HOME%\bin\java" goto haveJava
echo java does not exist as %JAVA_HOME%\bin\java
echo please fix the JAVA_HOME environment variable
:haveJava
"%JAVA_HOME%\bin\java" -classpath "%ASPECTJ_HOME%\lib\aspectjweaver.jar" "-Djava.system.class.loader=org.aspectj.weaver.loadtime.WeavingURLClassLoader" "-Daj.class.path=%ASPECTPATH%;%CLASSPATH%" "-Daj.aspect.path=%ASPECTPATH%" %*
Any clue?
Try this instead, for your java command:
java -cp .;C:\aspectj1.7\lib\aspectjrt.jar;C:\aspectj1.7\lib\aspectjtools.jar;C:\aspectj1.7\lib\aspectjweaver.jar;C:\aspectj1.7\lib\org.aspectj.matcher.jar Simple
You need to include the current directory (where you are compiling your source files to) in the classpath.

How to run java application without using IDE?

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!

Java - Library path error

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

Issue in creating and running a .jar file

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.

Categories