Using custom jar libraries in Command Prompt and IntelliJ IDEA - java

I am having a problem using the custom jar libraries (algs4.jar/stdlib.jar from http://algs4.cs.princeton.edu/home/) with the command prompt. I added the libraries to the IntelliJ classpath setting (project sturcture -> SDKs -> classpath) and am able to use their classes with no problems using IntelliJ.
However, I also want to be able to use these libraries in the command prompt. Even though I had no CLASSPATH variable set in my Windows settings I've been able to use javac/java in cmd with no problems when using the standard java libraries. (Probably because IntelliJ sets the CLASSPATH for all the standard libraries on every startup/compile to work systemwide). However, even after adding the 2 jars to the IntelliJ classpath setting, I wasn't able to use "javac" in cmd. I then created the CLASSPATH variable in windows settings, and added the jars to them. After this I was able to compile with javac with no problems. However, when I try to run the program in cmd, I get this:
"Error: Could not find or load main class ".
I get this error whenever I uses ANY of the jar libraries, including the standard ones, with which I didn't have problems prior to setting the Windows CLASSPATH. I guess what happens is once I set my own CLASSPATH this overrides the classpath set by intelliJ. When I removed the CLASSPATH, I was once again able to compile and run the standard libraries in cmd, but not the 2 custom libraries.
Please help!

You should make these libraries a part of the project by adding them to the module dependencies as a library instead of the JDK, then you can produce an artifact jar file that will have all the dependencies inside or near the jar and referenced via the jar manifest file classpath. See also the artifacts help section.

As this post points out, in this particular case the problem likely is due to the fact that that the classes in the library are "...placed in the default package (no package statement), and modern versions of java (at least 7 and 8) forbid to refer classes in default package, unless your own class is in default package too."
To solve the problem, I extracted the .java files from the jars (change the filetype from .jar to .zip and extract to a directory). I made up a package name, "com.sedge.stdlib", and modified the .java files as needed. For example, I modified the StdLib.java class from algs4 by placing a package directive at the top:
package com.sedge.stdlib;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
/**
* <i>Standard output</i>. This class provides methods for writing strings
* and numbers to standard output.
* <p>
* For additional documentation, see Section 1.5 of
* <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
*
* #author Robert Sedgewick
* #author Kevin Wayne
*/
public final class StdOut {
. . .

Related

Using Jsoup library on eclipse [duplicate]

I have just recently started using Eclipse and am running into problems trying to install external libraries. Following online tutorials, I add the .jar file to the classpath and I see it in the referenced libraries folder. Despite this, when trying to import, I get the error:
The package org.apache.commons is not accessible
For reference, I am trying to install the apache math commons library.
Your code probably has two issues.
First, the import statement is wrong since in Java you cannot add a package itself, but all classes of a package as follows (note .*; at the end):
import org.apache.commons.math4.linear.*;
or a specific class, e.g.
import org.apache.commons.math4.linear.FieldMatrix;
Second, you use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9 and you have Java 12.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)

How javac works while importing a package?

I am having a doubt.
My understanding is that jdk has [ jre + development tools (Java, javac, debugger etc.) + source code (src.zip) ].
Now working of java compiler is nothing to do with the running of class file.
If I am compiling a .java file then from where the java compiler is importing the package?
I could find the packages under jre.
If I do not opt to install jre while installing jdk, does that mean I will not be able to compile the java file having import statement?
Please help.
First, as a minor remark, a statement like
import java.util.List;
just introduces an abbreviation, allowing you to use the simple word List later in your code instead of the full class name java.util.List. So it's not so much the import statement itself, but the usage of a class like java.util.List that needs some explanation.
You understand correctly that, to compile your java file, the compiler needs some information about every class you use, and it typically finds this information in some jar file containing that class.
Now, where is this jar file containing the java.util.List class that the compiler reads? You're correct, it comes from the JRE, from the rt.jar that's part of the system classpath (the Java compiler itself is a java program that needs the basic classes itself, so wherever you successfully run javac, you always have an rt.jar available).
If your source code used a class from some other library, you'd have to specify that library on the javac command line, using an option like -cp.
Jdk = JRE + other tools like you mentioned.
When you are compiling your java file and you are using java inbuild library then it uses rt.jar to resolve dependency i.e import statements.
You can refer below link for the difference
What is the difference between JVM, JDK, JRE & OpenJDK?

Unable to use apache commons ftp import

I've been trying to create my own FTP client written in Java. I wrote one ages ago in C#, so I would like to try in Java. I created a user library for apache commons and added it to my build path. The problem is that when typing the import statement it doesn't recognise it. It finds a error at org, saying "import org cannot be resolved".
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
I am using eclipse as my IDE.
Here two screenshots showing the error:
The following line is probably missing in the module-info.java file:
requires org.apache.commons.lang3;
and in the Java Build Path the Classpath contains the broken (as you can see from the red error mark) item apache-commons-lang by mistake: select it and click Remove.
Since Java 9, modules can be specified, but this requires a correct module-info.java file and the libraries must be added to the Modulepath instead of to the Classpath. Without the module-info.java file it would be easier. Therefore, perhaps the simpler solution would be to delete module-info.java file.
In addition, the *-source.jar should be as Source attachment a child of the main JAR and the *-javadoc.jar is not needed with the source (but if, then as Javadoc location child of the main JAR).
Hint: If an error or a warning is shown with a light bulb, go to the line and click Ctrl+1 and Eclipse will suggest solutions for that problem.

Java not processing JUnit jar

I am unable to compile tests with JUnit. When I attempt to do so, I get this error:
package org.junit.jupiter.api does not exist
I get this error compiling the tests even if I put the .jar in the same directory and compile as follows:
javac -cp junit4-4.12.jar Tests.java
The contents of Test.java are:
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class Tests {
... several tests ...
It's not clear to me what the issue is, and as far as I can tell, it should work with the .jar -- it's the one from /usr/share/java, where it was installed when I installed junit.
As #DwB has already mentioned you have wrong junit version.
Here is what is jupiter in JUnit: http://junit.org/junit5/docs/current/user-guide/#overview-what-is-junit-5
In simple words JUnit Jupiter API is a set of new classes which were written and introduced in junit 5 version only. And ur trying to use 4 version.
And also i want to clarify some points.
even if I put the .jar in the same directory and compile as follows
It does not matter actually is your file in the same directory or not. Its all about it's path. If you are setting jar only by name of jar file (as you did) then your path becomes relative to your current directory from where u execute javac command. You can just use absolute path and run this command from every directory you want.
https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html (this one is for windows but for other os there are only minor changes in path writing)
If you get errors like package does not exist, classnotfound or anything similar then such kinds of errors almost always mean you have something wrong with your classpath or dependencies. In your case you simply had wrong version.
Now about finding necessary deps. In java world one of the main places for dependencies is maven central. Almost every opensource library can be found there and maven by default uses this repository to find and load dependencies (in your case these are jars) from there. Also you can use it to get necessary jars manually by simply using it's UI (https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api/5.0.0). There is download jar button.
Now if you know package or class but do not know in what dependency (jar for simplicity) it is located. In this case you can use http://grepcode.com or other resources which allow to search within available source code withit different repositories. In most cases this work. With juniper i did not manage to find smth there but in other cases this may help) Or the most simple case is just google package and in most cases it also will help to define entry point.
Now about solving ur issue. It seems that you will need as api as implentation. You will definitely need this one https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api/5.0.0 but it seems that you will need juniper-engine too. First try adding only API and then just go on adding necessary libraries according to errors. You can add multiple jars to cp (read provided class path guide from oracle).

"packaged does not exist" when compiling with javac

I am nearly complete with my internship work. One thing that is holding me back is compiling
I have a directory with three sub directories: lib, source, class. Within the source sub directory I have a GUI.
I try to run the following command to compile the GUI.
javac -cp .:lib\poi-3.11\poi-3.11-20141221.jar -d class\ .\src\GUI\*
Contents of some files withing the GUI are dependent upon Apache POI. When I run this command I get the following error:
src\GUI\CELL_TO_STRING.java:4: error: package org.apache.poi.ss.usermodel does not exist
import org.apache.poi.ss.usermodel.Cell;
Here is the imports of my java file CELL_TO_STRING:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
There are a few more but for brevity, I'll just list these. I believe this is some issue with specifying the class path to the Apache POI. Apache POI is a pretty big library with tons of jar files. So it will be hard to show you what exactly is in it. But if you could download for yourself to help out, that would be awesome.
From what I have seen on the web is "use maven", "use ant", "use this IDE". IDEs/Build Tools are very useful, I understand that. But I want to know how to properly build java programs through a terminal like ms-dos or shell. Having said that, please don't reply with the mentioned responses on other threads.
Please let me know if I need to give some more information.
Check the components page of Apache POI. Could you try after including poi-ooxml and poi-ooxml-shemas jars? These are required for ss it seems.

Categories