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.
Related
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.
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've never used any third party library before. What should I do after I downloaded jfreechart-1.0.14.tar.gz?
I don't know if I'm doing these things right:
1. Put the jcommon-1.0.17.jar and jfreechart-1.0.14.jar at the same directory as my source code.
2. Import needed class in the source code (e.g. import org.jfree.util.Rotation;)
Many articles tell you how to do this in IDEs. But instead of IDEs, I'm writing codes with vim and compile by myself. So, assume I didn't do any thing wrong, how should I compile the source code with javac and run the code with java?
Edit:
Here's my file layout:
./src
| - test.java
./lib
| - jcommon-1.0.17.jar
| - jfreechart-1.0.14.jar
I compile by
javac -cp "lib/*" -d classes/ src/test.java
then run by
java -cp classes:lib/jcommon-1.0.17.jar:jfreechart-1.0.14.jar test
However, some error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/general/PieDataset
How can I resolve this problem?
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/general/PieDataset
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.jfree.data.general.PieDataset
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
The libraries shouldn't be at the same place as the source code. If you don't want to use a build tool like Gradle yet, which would handle your library dependencies, then I suggest using the following layout:
project
src
.java files here, organized in a folder tree matching the package tree
classes
compiled .class files here
lib
.jar files here
To compile, go in the project directory and execute the following command:
javac -cp lib/jfreechart-1.0.14.jar:lib/jcommon-1.0.17.jar -d classes src/com/foo/bar/MyClass.java src/com/foo/bar/MyOtherClass.java
To run your app, execute the following command:
java -cp classes:lib/jfreechart-1.0.14.jar:lib/jcommon-1.0.17.jar com.foo.bar.MyClass
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).