How to import jar file by mac terminal - java

I try to import stdlib.jar in my project by terminal. The procedure is like this:
Move stdlib.jar to the folder "bin";
Add import bin.stdlib.* in the main class which residences in folder "src" and saved;
In terminal try src> javac -cp '../bin/:stdlib.jar' MyClass.java but failed to compile with the reason "package bin.stdlib does not exist". There are indeed many answers related to this issue online but I still cannot figure it out. Any advice is welcomed.

Related

Using javac with import

I try to compile this code:
package edu;
import java.io.*;
public class Main { ... }
with javac called from command line. I know that I need to do it this way:
javac -classpath /lib/* Main.java
and put .jar file with the 'java.io.*' classes in 'lib' folder in my project's directory.
Is my javac command correct - especially path to '/lib/*'?
How do I find the desired .jar file(s) so that I can copy them to my project's lib directory?
package java.io is part of core java, you do not have to put anything extra into your classpath

How to import java files from folders in linux?

In my linux, I have my java files that I coped from my windows eclipse project, and now I want to compile it in linux.
The folder structure is
PlutoMake.java
java-json.jar
Filter\ColorFilter.java
Filter\Darken.java
Filter\NoFilter.java
Filter\VividLight.java
The PlutoMake file has these imports and some others too like for json
import Filter.ColorFilter;
import Filter.Darken;
import Filter.NoFilter;
import Filter.VividLight;
But when I try to compile plutomake, it says
PlutoMake.java:12: package Filter does not exist
import Filter.ColorFilter;
I already use this to compile it:
javac -cp "java-json.jar" PlutoMake.java
and similarly for other ones too.
Does anyone know how to import it?
Thanks
You are getting package Filter does not exist error because the Filter.* classes are not present in the class path.
First compile the java files in side the folder Filter then compile the PlutoMake.java using javac -cp "java-json.jar;." PlutoMake.java
I'll advice to use some build tool like Maven or Gradle or Ant
If i understand the question correctly. In eclipse just go to fil > Import > Existing Projects into Workspace
Select your project and import all your files. Then hit finish.
Are they in the right subdirectories?
If you put /usr/share/'classpath', files defined with package Filter should be in /usr/share/Classes/Filter/
try setting java -classpath

I have import org.apache.commons.io.FileUtils; in my code and it works on eclipse but not in command line

I'm using an Apache package; I've add it's jar file location to the classpath environment variable. When I try to compile the code via the command line I get numerous errors including: package org.apache.commons.io does not exist
You are missing the jar that contains this package (in your case apache-commons.jar) on your classpath.
Try to export the packaged jar file with the dependend jars included. That should work.
Complile like this:
javac -cp .:common-io-xx.jar YourProgram.java
Run it like this:
java -cp .:common-io-xx.jar YourProgram
Actually the above option will not work sometimes.
At run time it might not find the class YourProgram :( Strange but true!
In that case just drop common-io-xx.jar under your
...\Java\jdk1\jre\lib\ext
Ofcourse that will work :)
Only wasted a few hours to get to that conclusion!

Import library is not resolved

I have tried to compile a java program, shorten.java from mp4parser site. But my jar file is unable to resolve the required import shown below.
This program needs to import library from iso mp4parser as mentioned below. In short I need jar file for shorten.java file to resolve these import.
import com.coremedia.iso.IsoFile;
import com.coremedia.iso.boxes.TimeToSampleBox;
import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.Track;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;
import com.googlecode.mp4parser.authoring.tracks.CroppedTrack;
I have downloaded the source code from the mp4parser site to create the jar file for these library.
In below image java->com->coremedia->iso and java->com->coremedia->iso->boxes having required java program
and java->com->googlecode->mp4parser->authoring etc. having other needed java program for these import
I have created the jar by following command-
cd java // moved in java folder
jar cfv mp4parser.jar *
now add these jar to my shorten.java program.
But I am getting error that import is not resolved.
****Do anybody can tell me where I am doing mistake ???****
Here is the directory where java codes for these imports are available.
I suggest you to this:
Create a quickstart java project with maven;
Add the dependencies that you want;
Put the java class in src/main/java
mvn clean install
If it don't work, try some IDE, like eclipse. Open the project and try to fix the imports... It's always possible that some lib has changed, and maybe they change the name of the package.

javac -classpath not doing the trick

I have a source file SerialTalk.java, in directory C:\javasrc\BattProj
This file imports classes from RXTXcomm.jar, eg.
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
...
RXTXcomm.jar is in the same directory as SerialTalk.java. I compile specifying a classpath pointing to the current directory:
javac -verbose -classpath . SerialTalk.java
Invariably, I get the following error. (Actually, many instances & variants of this error):
SerialTalk.java:3: error: package gnu.io does not exist
import gnu.io.CommPortIdentifier;
When I open the RXTXcomm.jar (eg. with 7-Zip) I can see the gnu.io structure, and the specific .class files that I'm trying to import.
So what am I doing wrong? The same .java (source) file has been compiled and run on another workstation within the Netbeans IDE. The difference here is I'm trying to compile it using javac from the command line. (Environment is Win7, 32 bit, jdk1.7.0_03)
So what am I doing wrong?
You're not putting the jar file on the class path. Putting the directory on the class path doesn't do it. That only tells javac where to find .class files in the directory structure, not jar files containing class files. You want:
javac -verbose -classpath .;RXTXcomm.jar SerialTalk.java

Categories

Resources