How to import a class that i wrote in java - java

So there is a folder in which there is the main.java file and a folder in which is a class,
how to import the class in main from the folder?

Direct folder import can't be achieved in java.
Following can be done
Package the other java class as jar
Include that jar in classpath of main program
Import the other java class in main program

Given your question, I'm assuming that you're a beginner. Here are the steps to follow for Eclipse IDE:
Under the class (which you want to import) go to: File -> Export -> Java -> Jar file.
Choose the resources to export and select a destination for the jar file.
In the project where you want to import the file, go to: Java Build Path -> Libraries -> Add External Jar -> Choose the jar file.
In the class where you need to use the file, write: import com.xxx.yyyy; (Package name).

Suppose we have the following:
LibA.Package1
SomeClass.class
LibA.Package2
Main.java
In your main file, you will need to use the full import path in the Main.java
import LibA.Folder1.SomeClass
See the following question for reference

Related

How to import a package from another folder?

I am trying to save all packages in lib/Package folder and my main program where i want to >import it is save in src file as shown in picture. How could i import Add package to perform
task. I m new in java.
the path of package is C:\Users\user_name\Documents\GitHub\NewProjectA\lib\packages
the path of main file is C:\Users\sumit\Documents\GitHub\NewProjectA\src
please help me to understand it in a easy way. That how can i import a package in the main
For that you need to create package under your src folder or create a diffrent moduke and import it
You should move the lib folder into the src folder. All code in a java project will be under the src file. Therefore the Source name.

Error importing package in Java

I am trying to sort my classes into packages but i can't import them.
My files are in the following folders:
- .java files are in C:\Java\Code\src\my\app\Timer
- .class files are in C:\Java\Code\compiled\my\app\Timer
In my class (timer) i've added package my.app;
Also, I have setted the CLASSPATH to look in both src and compiled folders.
Then, I have another folder where I put my "bigger" projects in:
- C:\Java\Projects\myProject
The problem is that when I try to import the class Timer into MyProject using import my.app.*; all I get is:
Error: package my.app does not exist
Culd you please give me a hand?
PS. My IDE is Dr.Java
I have found the problem.
It appears that Dr.Java ignores complitely the CLASSPATH variable. It is necessary to set in preferences where are the .class files.

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

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.

Problem in packages in importing in Eclipse

I am trying to import some existing projects into Eclipse.
The structures for their packages is:
Project/
/src
/java
/a
/b
/c
Once imported in the package explorer I see:
Project
src/java
--a
--b
--c
- AClass.java
This is ok, since the classes e.g. AClass.java are defined in package: a.b.c
But in one project the structure (once imported) becomes:
Project
src
--java
--a
--b
--c
- AClass.java
And that causes the error that AClass.java is defined to be in package a.b.c but it is actually under java.a.b.c
Why is this happening? Why in this specific project java is not ignored as part of package?
Thanks
How are you creating the Eclipse projects? It sounds like you just need to put "java" as a root on on the source path here, instead of "src". You can do this by editing the build path after the import process, of course.
Remove the existing source folders first. -right click -> menu -> build path -> remove from build path
then
Right click on the source folder. build path -> use as source folder.
Seems like your settings are pointing to the parent of the source folder so src is recognized as package by eclipse.
Wrong package name when using automatically added imports in Eclipse
call the package on the top of your import statements,
like if your class is in java/main/org/goal/Main.java
then the path is package java.main.org.goal;
else do Ctrl +1 and it suggest some quick help
import the necessary package from that
Use this sentence import java.io.*; at the top of java file. Otherwise, you have to create package folder.
Import statements:
In Java if a fully qualified name, which includes the package and the class name, is given then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class.
For example, the following line would ask compiler to load all the classes available in directory java_installation/java/io :
import java.io.*;

Categories

Resources