Problems exporting Jar using Weka - java

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.)

Related

Java - ClassNotFoundException: java.net.http.HttpClient

I'm attempting to run a jar file on a windows VM. When testing it using the CMD prompt, I get this error -
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.NoClassDefFoundError: java/net/http/HttpClient
at jarName.Main.removeExtinct(Main.java:250)
at jarName.Main.main(Main.java:93)
... 5 more
Caused by: java.lang.ClassNotFoundException: java.net.http.HttpClient
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
This is my MANIFEST.MF:
Manifest-Version: 1.0
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Rsrc-Class-Path: ./ selenium-server-standalone-3.7.1.jar aws-java-sdk-s3
-1.11.529.jar aws-java-sdk-kms-1.11.529.jar aws-java-sdk-core-1.11.529.
jar ion-java-1.0.2.jar jackson-dataformat-cbor-2.6.7.jar jmespath-java-
1.11.529.jar aws-java-sdk-dynamodb-1.11.529.jar httpclient-4.0.1.jar co
mmons-logging-1.1.1.jar commons-codec-1.3.jar httpcore-4.0.1.jar jackso
n-core-2.9.3.jar jackson-databind-2.9.3.jar jackson-annotations-2.9.0.j
ar postgresql-42.2.8.jar json-20190722.jar commons-net-3.6.jar jasypt-1
.9.0.jar ebay-oauth-java-client-1.1.0.jar okhttp-3.10.0.jar okio-1.14.0
.jar snakeyaml-1.20.jar joda-time-2.9.9.jar slf4j-api-1.7.25.jar seleni
um-java-3.11.0.jar selenium-api-3.11.0.jar selenium-chrome-driver-3.11.
0.jar selenium-edge-driver-3.11.0.jar selenium-firefox-driver-3.11.0.ja
r selenium-ie-driver-3.11.0.jar selenium-opera-driver-3.11.0.jar seleni
um-remote-driver-3.11.0.jar selenium-safari-driver-3.11.0.jar selenium-
support-3.11.0.jar byte-buddy-1.7.9.jar commons-exec-1.3.jar guava-23.6
-jre.jar jsr305-1.3.9.jar checker-compat-qual-2.0.0.jar error_prone_ann
otations-2.1.3.jar j2objc-annotations-1.1.jar animal-sniffer-annotation
s-1.14.jar jjwt-0.9.0.jar commons-lang3-3.7.jar gson-2.8.6.jar
Rsrc-Main-Class: jarName.Main
Class-Path: .
I think the class it's saying it can't find is httpclient-4.0.1.jar which is on the 6th line if the MANIFEST file. The only issue I can think of is the Class-Path at the end of the MANIFEST being just .
I also unzipped the .jar file, to make sure the relevant .jar files are actually present, which it is (assuming httpclient-4.0.1.jar is the correct file).
I can run other .jar files on this VM, though none with this class in it. I've updated Java as well, just in case, which didn't help.
httpclient-4.0.1.jar most probably means version v4.0.1 of org.apache.httpcomponents:httpclient.
And java.net.http.HttpClient was intriduced since 11 version of JDK.
Didn't you forget to use correct version of jre/jvm?

NoClassDefFoundError using bluetooth in java

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

How do I get started with JFreeChart?

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

com4j wrapper gen problems

I am trying to generate wrapper for com4j using tlbimp-20110320.jar. I also tried a 2008 version with the same errors. I get the same errors even when I try running against the example in the tut.
C:\Users\matthew\Documents\NetBeansProjects\test>"C:\Program Files (x86)\Java\jr
e7\bin\java.exe" -jar tlbimp-20110320.jar -o excel -p excel "C:\Program Files (x
86)\Microsoft Office\Office14\EXCEL.EXE"
Exception in thread "main" java.lang.NoClassDefFoundError: org/kohsuke/args4j/Cm
dLineException
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.kohsuke.args4j.CmdLineException
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
After Iceman provided a link to a required library I was able to get it to run a little. However I had to copy the arg4j contents into the tlbimp jar, and the com4j contents into the tlbimp jar as well. Now I get
C:\Users\matthew\Documents\NetBeansProjects\test>"C:\Program Files (x86)\Java\jr
e7\bin\java.exe" -jar tlbimp-20110320.jar -o excel -p excel "C:\Program Files (x
86)\Microsoft Office\Office14\EXCEL.EXE"
Generating definitions from Excel
A referenced type library "Office" is generated into the same package "excel"
Unable to handle the type SAVEARRAY(Variant)*
method ConnectData
interface IRtdServer
A referenced type library "VBIDE" is generated into the same package "excel"
Exception in thread "main" java.lang.NullPointerException
at com4j.tlbimp.driver.Driver$1.resolve(Driver.java:64)
at com4j.tlbimp.Generator$LibBinder.<init>(Generator.java:382)
at com4j.tlbimp.Generator.getTypeLibInfo(Generator.java:686)
at com4j.tlbimp.Generator.getTypeName(Generator.java:663)
at com4j.tlbimp.TypeBinding.bind(TypeBinding.java:173)
at com4j.tlbimp.MethodBinder.declareReturnType(MethodBinder.java:542)
at com4j.tlbimp.MethodBinder.declareWithDefaults(MethodBinder.java:399)
at com4j.tlbimp.MethodBinder.declare(MethodBinder.java:118)
at com4j.tlbimp.InvocableInterfaceGenerator.generateMethod(InvocableInte
rfaceGenerator.java:39)
at com4j.tlbimp.InterfaceGenerator.generate(InterfaceGenerator.java:94)
at com4j.tlbimp.Generator$LibBinder.generate(Generator.java:491)
at com4j.tlbimp.Generator.generate(Generator.java:112)
at com4j.tlbimp.driver.Driver.run(Driver.java:107)
at com4j.tlbimp.driver.Main.doMain(Main.java:132)
at com4j.tlbimp.driver.Main.main(Main.java:56)
tlbimp-20110320.jar is required args4j-2.0.1.jar and com4j-20110320.jar
Note: com4j-20110320.jar, it is necessary to change the name to com4j.jar
Additional Info
The tlbimp of the latest version (tlbimp-2.1.jar), requires args4j-2.0.8.jar and com4j-2.1.jar.
In this version, file name change is not required.
From the stack trace, it looks like you are missing dependent jars from the java class path. The program seems to be looking for Koshuke's args4j library - http://args4j.kohsuke.org/.
Unjar tlbimp.jar and check out its manifest. It has a classpath where args4j and com4j must be in the same directory as tlbimp.jar and require specific names. The specifics of the name depend on the build and may or may not include version number. Name your com4j and arg4j as indicated and you're good to go.

Error "main class not found"

All,
My classpath has been set to the following folder:
CLASSPATH = .;C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program Files\Java\jdk1.6.0_21\bin\project
All my java files and class files are under project folder listed in CLASSPATH.
Yet I am getting the following error while running a DriverClass in project folder:
C:\Program Files\Java\jdk1.6.0_21\bin\project>java DriverClass
Exception in thread "main" java.lang.NoClassDefFoundError: DriverClass (wrong name: project/DriverClass)
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: DriverClass. Program will exit.
Can anyone please help me understand the reason here?
You're inside the package. You should be sitting in the package root. Go one folder up.
cd ..
Then reexecute it using java project.DriverClass.
That said, you should prefer not to use the CLASSPATH environment variable. Yours is currently also invalid. There are spaces in unquoted pathnames. Also, the JDK/bin folder isn't supposed to go in the classpath. Just use the -cp argument like java -cp . project.DriverClass (while sitting in the package root folder).
Try:
C:\Program Files\Java\jdk1.6.0_21\bin>java project.DriverClass
assuming your package is project
After compiling if we get a main class not found error, It is simple try this
set classpath=%classpath%;.;
type this in cmd and run it..problem will be resolved

Categories