Import Apache Commons Math Library (Beginner) - java

I want to be able to use the Apache Commons Math Library in Java but I cannot get it to work correctly and the main site is frustratingly unhelpful (at least for a novice like me) and I haven't been able to find a solution on here yet.
I went to http://commons.apache.org/proper/commons-math/download_math.cgi
downloaded the first option commons-math3-3.6.1-bin.tar.gz
unzipped it and put it into the folder with the java class that I am trying to build.
I then did the command import org.apache.commons.math3;
But I get Error: package org.apache.commons does not exist
Could someone explain (preferably in detail that not even a novice would misunderstand) why this isn't working and what I should do?
Thanks!

First you need to download jar from repository
https://mvnrepository.com/artifact/org.apache.commons/commons-math3/3.6.1
put it to folder test, in same folder create file Test.java with class Test something like this
import org.apache.commons.math3.analysis.function.Abs;
class Test {
public static void main (String ... args) {
Abs abs = new Abs();
System.out.println(abs.value(-10.0d));
}
}
after that compile it with command javac -cp "commons-math3-3.6.1.jar" Test.java
and run it java -cp ".;commons-math3-3.6.1.jar" Test
output will be 10.0

Related

How do I use a .jar file in my project and compile it without an IDE?

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

Java: How to build and use packages from the terminal

I've used Eclipse for most of my Java programming, but I'm now volunteering at a prison to teach inmates, and they don't have access to any IDE so we have to do everything from the terminal. (I'm also primarily a Math teacher but doing what I can for the CompSci stuff.) As I understand the file structure in Eclipse, when you have a package called, say, fsk, you place it in a hierarchy like
fsk
src
Sort.java
Main.java
bin
Sort.class
Main.class
where the Main class calls on the Sort class, so to compile the source code at the terminal I write
$~/fsk/src: javac Sort.java -d ../bin
The Sort.java file starts with
package fsk;
...
and the Main.java file has the package name and makes a simple call to the Sort class
package fsk;
import java.util.List;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
List<Integer> li = new LinkedList<Integer>();
li.add(1);
Sort<Integer> s = new Sort<Integer>();
s.insertionSort(li);
}
}
The Sort.java file seems to compile just fine, but when I try to compile the Main.java file I get the error that it cannot find the symbol "Sort".
If I do all of this without any package declaration, everything works just fine. Besides modeling this after how I understood Eclipse to work I have also been using https://docs.oracle.com/javase/tutorial/java/package/managingfiles.html and thought I was faithfully reproducing the structure and code, but apparently not.
[Edit: Correction, I have been mostly going off of the Eclipse structure as I understand it, I'm now noticing that when the Oracle tutorial structures this it uses a structure like
src
fsk
Sort.java
Main.java
bin
fsk
Sort.class
Main.class
So I suppose my question now is: am I misunderstanding how Eclipse structures the files, and do I need to instead structure them as in the above?]
The structure is fine. You'll want to compile all the Java files with one javac invocation.
javac fsk/*.java -d ../bin
This will get annoying once you start having multiple packages. Long term you should use a build tool such as Maven, Ant, or Make. Compiling files by hand gets mighty tiresome.

java.lang.NoClassDefFoundError with jar file in terminal

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

Using class files from a jar file that already has the path built

I'm a little lost here. We were given a jar file which contained 3 different .class files in it. I successfully built the path to the jar file, but I don't know how to use them. The class files obviously contain methods that can be used, but I've never worked with a jar file before so I don't know how to use them. I'm not sure if I have to import them somehow, how to instantiate them or anything. I have searched for tutorials to no avail.
All I'm looking for is some guidance so I can get moving on this project. If it is a case where I have to import them somehow how would I do it? For example, I have WikiEdits.class contained in WikiEdits.jar. The name of my main class is P2. Can someone show me a brief example of how this works?
Add the jar to your classpath, if you are using an IDE.
Then, a java class that uses it would look like something like this:
package p2;
import blah.WikiEdits; //references a class in the jar
public final class P2 { //(this is a strange name for a class, by the way)
public static void main(String... args){
//builds a new object of the given class
WikiEdits thing = new WikiEdits();
}
}
If you are using the command line, these examples may help:
http://www.javapractices.com/topic/TopicAction.do?Id=243
you need to add WikiEdits.jat to your path project, then import and instanciate the class.
import WikiEdits
P2 p = new P2();
p.somemethod();
Static class:
WikiEdit.someMethod();
In your java class add the relevant imports from the jar. And then from command line, you can compile and run your class using the classes from jar by definining the right classpath:
Compilation
on windows:
javac -cp .;pathtoyourjar YourClass.java
on linux:
javac -cp .:pathtoyourjar YourClass.java
Execution
on windows:
java -cp .;pathtoyourjar YourClass
on linux:
java -cp .:pathtoyourjar YourClass
If you are using Eclipse then follow this link to know the steps to add jar to your project:
http://www.cs.duke.edu/courses/cps004g/fall05/assign/final/addlibrary.html

java import "cannot find symbol"

I'm about porting a linux tool to windows. The tool works fine on linux system, but now on windows I get this "cannot find symbol" error.
I have this little main class:
package foo;
import foo.bar;
public class Main {
public static void main(String[] args) throws Exception {
bar.getInstance();
}
}
and the error appears now, while doing javac Main.java:
import foo.bar: cannot find symbol
^
symbol: class bar
location: package foo
Main.java and bar.java are in the same directory.
what am I missing?
For one thing, bar should be called Bar to be idiomatic...
Ideally, you should compile from the directory above Main.java, like this:
javac -d out foo/Main.java foo/Bar.java
That will create a directory called "out" containing another directory "foo", which will contain Main.class and Bar.class. So from the parent directory again, you could run:
java -cp out foo.Main
The source locations don't have to match the package structure. You could just call javac from the directory containing Main.java and Bar.java like this:
javac -cp out Main.java Bar.java
(And then run it in the same way as before) However, it's generally a much better idea to structure your source code according to packages.
You may well find it easier to use an IDE (Eclipse or NetBeans, for example) which will handle all the compilation etc for you. If you do want to build a real project from the command line, you should probably look into using a full build system such as Ant or Maven.
(Note that you'd get the same error on Linux as on Windows, if you tried to compile in the same way.)
If any of you have worked on a kotlin project and then working on a complete Java project without the support of kotlin in gradle, and you put a class in kotlin into the Java project and try to import that Kotlin class, you will get this import issue. Not with reference to the exact question, but someone may find it helpful.
Example : trying to import SomeClass.kt in MainActivity.java will throw this error.
I think you need to compile properly the class bar before attempting to compile foo, this is, generate the class file in the proper package structure.

Categories