Package does not exist when compiling - java

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

Related

Load Java libraries via VSC?

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

How to compile Java class with .jar dependencies on Linux command line

I'm a CS Student working with my first .jar file and I'm having trouble getting my class that's dependent on it to compile. I'm using VSCode as my code editor, and I've added the .jar file to the "Referenced Libraries" section of my java project. The class I'm trying to run is a fairly simple class generated from our textbook, with an accompanying .jar file that has a bunch of custom libraries on it. I can see the contents of the jar in the VSCode java project browser, but when I try to compile I get import and symbol not found errors. I usually compile and run my projects in the terminal window (running Ubuntu 18.04 over Windows using WSL) by simply typing "javac MyClass.java". I would expect that if the jar has been loaded into VSCode, then it should compile the same way, but that doesn't appear to be the case. I've tried the recommended syntax "javac -cp /lib/myjar.jar MyClass.java" and "javac -jar /lib/myjar.jar MyClass.java" but neither works for me.
I'm fairly certain there's something simple I'm missing, as I don't have any experience working with jars, and certainly not in VSCode. Anyone out there that has an idea what I'm missing?

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.

trying to understand a package does not exist error

When I run javac on an ubuntu linux system I get a "package does not exist" error from the line
import org.voltdb.ProcInfo
From the answer below, I know that I need to add packages from "org/voltdb/ProcInfo" to my javac class path to resolve this error.
Java Package Does Not Exist Error
But when I search for the system for such a directory structure I don't find it.
prompt$cd /
prompt$sudo find / -type d -path 'org/voltdb/ProcInfo' //returns no output
I downloaded VoltDB community edition 3.5 and am trying to compile and run a voltdb project. It seems unlike that Volt's .tar does not include the necessary java libraries -- but that seems to be what is going on. Am I understanding what is happening correctly or am I missing something? I have only working knowledge of Java and Linux.
You need to add the VoltDB .jar file to the classpath

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