Load Java libraries via VSC? - java

I'm very beginner in Java and for that reason I'm having issues to understand how the imports work...
The problem is to import the Ini library to a project in VSC. I've download the jar from ini4j and place it in the folder Referenced Libraries of the project and a .vscode and lib folders were created.
In the class file I try to import it: import org.ini4j.*; and use it.
But when I compile the code it seems like it doesn't find it as you can see here.
Before asking the question I did some research and tried to follow tutorials that said to introduce in .vscode\settings.json this code:
"java.project.referencedLibraries": [
"library/**/*.jar"
"ini4j-0.5.4/ini4j-0.5.4.jar",
"ini4j-0.5.4/ini4j-0.5.4-jdk14.jar"
]
Do you have a solution for my problem please? Is my import poorly done or how I use the library in the call?
It seems like VSC recognises the library but not Windows...

The problem is the way you are compiling the program. If you want to compile it from the terminal and then run it, you need to specify what libraries you are using. I would recommend you to add Java extension to you VS code, but if you insist on compiling it from cmd you need to do:
For Windows machine
javac -cp ".;first.jar;second.jar;third.jar" *.java
And run it as
java -cp ".;first.jar;second.jar;third.jar" Main.java

Related

Getting a package does not exist error when compiling Google Cloud libraries in terminal with Java

I have a java file under src/ that has the following import because I would like to use Google Cloud's translate API:
import com.google.cloud.translate.*;
I have my main method in Main.java. I have all the necessary .jar files stored in a folder called "Cloud" and in the terminal, I try to compile the code by running:
$ javac -cp "Cloud/*.jar" src/*.java
But I get the following error:
src/Main.java:4: error: package com.google.cloud.translate does not exist
import com.google.cloud.translate.*;
^
I'm not sure why since I imported all the dependencies and set the classpath. It all works when I run the program in the IDE Eclipse, but it doesn't when I try to compile it in the terminal. Any ideas why?
I will post my comment as an answer as it is too long for the comment section.
When checking the official documentation regarding the Translating API, it is stated before trying to use it to follow the Translation Quickstart Using Client Libraries for the set-up process, as it might help.
Additionally, I stumbled across this SO post indicating that the issue could lay on running the javac command outside of the src folder.
I hope this information helps.

Having trouble importing packages in java

I'm in the process of a personal project I'm working on, and I'm trying to import packages I've created in Java.
Say that my directory is C:\Users\B\Desktop\gamingResearch\pokemonGames and in that directory I have the files Battle.java, Move.java, and another folder called pokemon. In my header for Battle.java, I have
import pokemonGames.trainerClass.Trainer;
import pokemonGames.pokemon.*;
And I try to compile the class with javac -sourcepath .:/Desktop/gamingResearch/pokemonGames Battle.java
But I still get an error saying
error: package pokemonGames.pokemon does not exist.
I'm running into the same errors in other classes regarding packages that I've made. Is there something that I'm forgetting to do?
Your sourcepath is wrong.
It should be:
-sourcepath /Desktop/gamingResearch
Hope it helps.
If you program in java, you do need to add libraries (like pokemonGames) to your classpath before compiling your application, like already stated in the comment by cricket_007.
To make things slightly simpler, you could use a build tool like gradle or maven or even just use an IDE (like IntelliJ) to build your application. Please note, that I recommend a build tool as well as using an IDE, so that you are able to build your application on other machines with different IDEs.

What is the difference between the Apache sources.jar and .jar files?

I'm curious about Apache commons-io, why do they include a sources-jar inside the code package. We will not compile the program like so :
javac -cp .;.\lib\commons-io-2.4-sources.jar myCode.java
But we compile it like this :
javac -cp .;.\lib\commons-io-2.4.jar myCode.java
So why do the libraries also include a -sources jar in the download code ? I'm guessing it's for studying the source code, if we want to add/improve ?
The source JAR is so you can read the code is you want to. If you use an IDE, it can know to down load this JAR and if you look at a class in it, it will show your the source. esp useful when debugging a program. If you are not using an IDE, you can unpack the source and read it to understand what it is doing.
The reason the source is not included in the compiled JAR is so it can be easily dropped if all your are doing is running the program e.g. in production.

Package does not exist when compiling

I´ve made a Java Project on Windows and now I want to use it under Linux.
Since I am working with Files, I "need" the org.apache.commons.io.FilenameUtils package. Under Windows I am working with IntelliJ, so it manages everything for me. I am sharing the Directory of the Project via VirtualBox shared folder. When I try to compile the Main class I get
error: package org.apache.commons.io does not exist
import org.apache.commons.io.FilenameUtils;
^
I already tried javac -classpath . Main.java but I just get the same Error.
To be honest I am completely lost from this Point on, althought I already googled the Problem for a bit. I run Debian and Java JDK 8
You need to have the Commons IO Jar in the classpath when compiling, for example:
javac -cp .:path_to_commons_io_jar Main.java

javac: package org.apache.derby.client.am does not exist

I'm trying to compile a java program that is using JavaDB/Derby. On the command line I can run
java org.apache.derby.tools.sysinfo
without errors, but the following line in several of the files causes the error in my question title:
import org.apache.derby.client.am.SqlException;
causes
package org.apache.derby.client.am does not exist
I've done a fresh installation of JavaDB, but I don't think that matters. I've compiled this project once before, and I KNOW I didn't have JavaDB installed. I just had a directory at the top level of the project folder called lib with all of derby's .jar files inside. And I'm pretty sure I didn't have to set any environment variables either.
How can I fix this error? If I need to provide any more information, I will be happy to do so.
I'm using Windows 7 and jdk1.7
Sounds like you have an issue with the JavaDB JARs not being on your classpath. Make sure you specify them using -cp or -classpath on your javac command.

Categories

Resources