IntelliJ program won't compile in terminal due to .jar file - java

I have used the more student-oriented IDE DrJava before, and did not have trouble running programs dependent on .jar files in the terminal/command line.
Right now I am writing a simple program to find eigenvalues of matrices in IntelliJ, and although I have followed the steps listed in the stack overflow question "Importing jar file into IntelliJ Idea?" to add the .jar file I am using as a dependency, I am getting errors like this when I try to compile the program in terminal:
javac eigenvalues.java
eigenvalues.java:11: error: package Jama does not exist
import Jama.*;
^
and etc. (more errors of the form such-and-such does not exist).
My program has multiple classes and I only need the .jar file for one of them.
The program compiles fine within intelliJ, just not from terminal.

You can make it work by two ways:
1. You can try by putting the required JAR in the ext folder: Below is the path to ext folder, then try to compile.
On Linux Systems - /urs/lib/JDK/jre/lib/ext/
On Windows Systems- C:/Program Files/Java/jdk..0.1/jre/lib/ext/
You can add the path of that required jar to the CLASSPATH system variable also.
You can compile the program by dynamically providing the classpath with javac command as below:
javac -classpath "<Path to the JAR>" YourProgram.java
There is a very good link on the classpath.
Hope It may work.

Related

Java can't compile in terminal because of external libraries

I'm on Ubuntu 22.04 trying to run my code in the terminal.
The program works without problems in VScode, also when running multiple instances.
The program is consisted of six class files.
The trouble occurs when I try and run it with terminal. When compiling the java file
with javac it shows errors at places where I use the external libraries.
If I compile it with VScode and run the class file in terminal, I get the following error java.lang.ClassNotFoundException
This is causing me problems since I'm also supposed to dockerize the program.
You can add the following code in your setting.json file "java.project.outputPath": "bin",
This will be the .class file generated by VS Code in the bin folder of the same directory when running the Java code.
You can use the java command after entering the file directory with the cd command.
This generally indicates that the class path with which you're compiling your program does not include the correct paths to your libraries. Assuming your libraries are jar files, your javac command should look something like this:
javac -cp libs/lib1.jar:libs/lib2.jar srcs/*.java
where libs/ is the relative path to your libraries and srcs/ is the relative path to your own java files.
And when you run the program, make sure your class path includes both the locations of your libraries and the location of your class files (which in this case would be the current directory):
java -cp .:libs/lib1.jar:libs/lib2.jar <MainClass>

Can't run java file (having trouble with classpath fixes)

I'm remoted into a Linux machine that I don't own from my Windows machine. I've got 2 java files:
DBConnect.java
Main.java
I compile and run them fine on my machine and in my IDE (I'm using NetBeans). When I copy them to the remote Linux machine, the place they're located is:
/home/NETID/myname/430
I compile them using:
javac *.java
They compile successfully, which creates two new files:
DBConnect.class
Main.class
I then attempt to run Main.class. With each of the following commands, I get the error "Error: Could not find or load main class Main"
java Main
java <pkg>.Main
java <pkg>/Main
In my case, in NetBeans, my project is called MyProject. In the directory structure on the left-hand side of the IDE window, there is MyProject. Under that is a file called Source Packages. In that file is another thing called pkgMyProject. When I expand that, I see my two java files.
Also, at the top of both Main.java and DBConnect.java, there is:
package pkgMyProject;
I've read some other questions here referring to this issue, but I'm having trouble understanding the answers. Also, many of the answers don't apply because I don't think I can set the classpath on the remote machine.
When I check my classpath in my terminal (when I'm remotely connected) using...
echo ${CLASSPATH}
...nothing is displayed.
How can I find my classpath, and how can I run these files?
I think you are missing the package folder. You need to create a directory in the Linux machine with the name pkgMyProject. Then, run javac pkgMyProject/*.java and run java -cp . pkgMyProject.Main after that.
Update: You can add the -cp to set the classpath to be used in the java command.

Unable to compile, classpath not set, package does not exist?

Normally I can use javac to compile and java to run perfectly fine, they are set correctly into my computer's classpath. In this instance however, one of my files Graph.java is trying to import a jar file in the same directory Heap.jar.
When I try to use javac *.java, it tells me package mycs1 does not exist.
Normally in my lab on Linux machines, we can use export CLASSPATH=mycs1.jar:. and then javac will work normally again, and just add more files if there are more jars.
My problem is that this command does not work on my home computer running Windows 10. I am trying to set the classpath of multiple jars. This is similar to this question:
Unable to execute jar file despite having PATH and CLASSPATH set
But despite reading that, I still cannot find a way to set the path of multiple jar files in such a way that javac will work.
Is this possible? If not, what alternative command can I use?

Compile and Run java program in linux with path to the .java file and path to external jars

Yesterday I solved a problem with an answer here on stackoverflow. But I ended up with another problem, I will try to be clear:
I have a project folder in the /home/demo/Desktop/xlsToCsv/ directory where inside of it is the java file "xlsToCsv.java" and another directory with the external jars that I need in /home/demo/Desktop/xlsToCsv/jars.
Now I need to compile and run my program. Yesterday I ran a command that assumed that I was already inside of /home/demo/Desktop/xlsToCsv/, and the commands were:
javac -cp ".:./jars/*" xlsToCsv.java
java -cp ".:./jars/*" xlsToCsv
The problem was solved and I was able to run my program without any problems. But, my program was supposed to run from the root directory, ie, the directory where it is when I open the linux terminal without the need to make a "cd" command.
So, when I open the terminal the path to the .java file is:
/home/demo/Desktop/xlsToCsv/
And the path to jars folder is:
/home/demo/Desktop/xlsToCsv/jars/*
Can someone explain to me what I have to do, and the reason so? Because more that run the program, I want to know the reasons and understand the classpath mechanism in java.
Avoid using relative classpath. and instread of "./jars/" use the absolute path "/home/demo/Desktop/xlsToCsv/jars/"
EDIT:
javac -cp "/home/demo/Desktop/xlsToCsv/jars/*" /home/demo/Desktop/xlsToCsv/xlsToCsv.java
java -cp "/home/demo/Desktop/xlsToCsv/:/home/demo/Desktop/xlsToCsv/jars/*" xlsToCsv

How do I generate .class file if I have external Jar dependencies? Javac does not work [duplicate]

This question already has an answer here:
How do I compile a java file that has jar dependencies?
(1 answer)
Closed 8 years ago.
Background: Running on an ec2 instance not eclipse.
I have a java program that has external Jar dependencies. I want to create an executable jar file for this java program (being able to package the external jar files into this jar file would be icing on the cake)
The problem is that for me to use the java cf or cmf command I need to have a .class file. But, when I try javac HKP.java it throws a bunch of errors saying "xyz does not exist or cannot recognize symbol" which I'm guessing is because it has no idea about the external jar dependencies.
I have tried doing javac -sourcepath /home/ec2-user/apps/HKP/lib/ HKP.java, but it throws the exact same errors as before.
EDIT I have tried javac -classpath file1.jar, file2.jar, file3.jar HKP.java -I have also tried this with ";" in between. both return errors saying -bash file2.jar command not found, so instead of continuing the argument, bash is recognizing the multiple jar files as commands.This is the initial problem I had that caused me to post here.
Any help is much appreciated. Thank you!
If you want to compile a java source file into a class, you need to provide all classes which are used in that code on the classpath of the compiler.
If the classes come from (already compiled) external JARs, you typically do that by specifying javac -classpath with a list of JAR files, separated by : (on Linux). But you should really think about using an IDE or at least a maven build file (Which has the benefit, it can even download those JARs for you).
Both (Eclipse IDE and Maven build system) can also generate a ueber-jar with the external classes in there for easy execution.
try to Add the path in system variable and restart your computer.

Categories