How do I include a class from a package in another location - java

I have a Stack.java file stored in
C:\Users\Aaditya\Documents\Github\Data Structures\com\stack\Stack.java
In the declaration of the file, I have given this
package com.stack;
So, now I have a Parantheses.java file stored in
C:\Users\Aaditya\Documents\Github\Data Structures\Parantheses\Parantheses.java
And now when I have the below code in this file, and subsequently I compile it,
import com.stack.*;
I get the following error
C:\Users\Aaditya\Documents\GitHub\Data Structures\Parantheses>javac *.java
Parantheses.java:1: error: package com.stack does not exist
import com.stack.*;
^
Can anyone sort this error out for me.
PS:
When I put all the java files in one folder and then compile them (without the 'import' and 'package' thing), I dont get any error. Program runs succesfully.
Thanks. :)

Try using -sourcepath while using javac which will allow you to include files from different location.
Links :
http://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html
http://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html#CBHHCGFB
On the other hand better way to manage dependencies is maven.

Related

"Package Doesn't Exist" - Error if I try to include a JAR-File [java compiler]

My problem is the following:
I am sort of new to programming and I wanted to include the guava.jar file I found on the internet (to use the ImmutableList.copyof()-method). I also have additional packages that I want to include (I wrote a few classes that handles stuff that's relevant for the project).
I implemented the following structure:
Location of the Main-Method that imports the AdditionalClass:
src/com/chess/engine/Main.java
The location of the AdditionalClass-method that imports the guava-jar :
src/com/chess/engine/board/MyAdditionalClass.java
If I compile my project from the "src"-directory via javac -cp com/guava.jar com/chess/engine/Main.java and I get the following error-message:
com/chess/engine/Main.java:3: error: package com.chess.engine.board does not exist
import com.chess.engine.board.*;
^
I think that the problem is that I set the classpath to the guava.jar file and that's why the compiler suddenly doesn't find the AdditionalClass anymore, but I don't know how to change.
If I don't include (...) -cd com/guava.jar (...) and compile by using java com/chess/engine/Main.java the AdditionalClass will be recognized by the compiler but I get a bunch of new errors because the compiler can't find the guava.jar and therefore has no idea what to do with the "ImmutableList.copyOf()" method I implemented.

How do I add a small additional package to a class library in Java?

I have a package called org.mine.level1.level2
I want to add a package called org.mine.helper with a class called org.mine.helper.Calc
So I went to java/org/mine where the directory 'level1' already is and created 'helper'. Then I created Calc.java and put a class in it. I also added usage of the class to org.mine.level1.level2 code (a couple of java files in there).
When javac compiled the modified level2 code it said that the org.mine.helper.Calc file didn't exist.
I looked at the existing tree and there was a build.xml file which said
<property name="src" location="java" />
which is the parent of the org directory. So it seemed like whatever is reading build.xml doesn't need to know the names of all the java files. That indicated to me I could add a directory at will and it would just be incorporated.
In tutorials that have multiple packages they always run javac on each file, cause the class files to go in some directory and put that in a class path. I guessed that build.xml is defining all that and I could just add my stuff.
What can I do to get my new java code compiled for the benefit of the places I use it?
Error messages:
org/mine/level1/level2/Usage.java:8: error: package org.mine.helper does not exist
import org.mine.helper.Calc;
^
org/mine/level1/level2/Usage.java:74: error: package org.mine.helper does not exist
org.mine.helper.Calc.compute();
You said:
So I went to java/org/mine where the directory 'level1' already is and created 'helper'. Then I created Calc.java and put a class in it.
But if you wanted to add a source file to the package org.mine.helper, then you should have gone to org/mine/helper, and created Calc.java there. So, you should either make this change, or, if you want to refer to the Calc class as it currently is located, then use this import statement:
import org.mine.helper.level1.Calc;

How to create a Java package from downloaded source in Linux

I downloaded an external library, common-codecs, and am trying to create a package from the downloaded source code so that i can import and use it in java class files. How would i go about doing this?
I moved the downloaded directory into the same directory as my java class files.
What I've tried so far:
package commons-codec-1.11-src;
I place this at the head of my java class file
Then i try and compile the file using javac in the Linux terminal
javac -cp ~/Documents/javapractice/commons-codec-1.11-src ~/Documents/javapractice/File.java
I get a "interface, class, or enum required error" and the compiler error points to the package statement in the java class file.
code:
import java.util.*
package commons-codec-1.11-src;
public class File
{
........
}
Just to clear things up commons-codec-1.11-src is source code I downloaded and is now a directory in the same directory as File.java
Any help would be greatly appreciated! Thank You!
I downloaded an external library, common-codecs, and am trying to
create a package from the downloaded source code so that i can import
and use it in java class files. How would i go about doing this?
You don't need and you should not package the source code of the external library in your application.
Extracting dependency classes in your own application is a very corner use case and it should done only as you have no choice.
What you need is adding the jar that contains the compiled classes in your classpath at compilation (javac command) and at runtime (java command).
Supposing that the jar is named commons-codec-1.11.jar, to compile your File.java class you should execute :
javac -cp ~/Documents/javapractice/commons-codec-1.11.jar /~/Documents/javapractice/File.java
The File.java declaration is not correct either.
The package declaration has to happen before the import declaration and the package and import values are not correct either.
It should be something as :
package javapractice;
import java.util.*;
public class File {
........
}
About import from the third party library, you need to import classes you use in File class.
You cannot import the whole package as you try to do.
I think that you should try to understand javac/java bases and start with an IDE to make things easier.

Package Utility doesn't exist error

I'm trying the sourcecode from jLDADMM which is GibbsSamplingLDA.java, but i cannot compile it because i get this following error:
package utility doesn't exist
maybe any of you ever experience the same problem? or maybe anyone know what is package utility and how to use it?
any help will be appreciated.
did you try with eclipse, This might works in eclipse. Wont compile on command line – says imported package does not exist:
Have two classes in two different packages (no inheritance)
class A SpecialDelivery – PKG = com.mgm2.specialdelivery
– Class B (UrgentMessage) has main() imports com.mgm2.specialdelivery.SpecialDelivery
c:\ src\com\mgm2 – has both specialdelivery and urgentmessage package paths -compile SpecialDelivery.java OK
Try to compile UrgentMessage from directory with both specialdelivery and urgentmessa pat h names so they are visible and get package specialdelivery does not exist.
To compile UrgentMessage.java use command
c:\src\com\mgm2 javac urgentmessage/UrgentMessage.java then it doesn’t reconize specialdelviery package even though I am compiling from the directory with both of them there.

Java using classes from jar

This must be a super overasked question. Although here goes:
I have a java file for testing around (hworld.java) and am trying to import conio.jar, a JAR which is a wrapper of Conio. The JAR contains only one class file (conio.class) and META-INF. Trying to do import conio.* or import conio.conio shows me this:
C:\Documents and Settings\Nick\Desktop>javac -cp *.jar; hworld.java
hworld.java:3: error: package conio does not exist
import conio.*;
^
1 error
And compiling it like javac -cp conio.jar hworld.java still errors out while compiling. I even extracted the jar and had conio.class in the same directory as hworld.java but to no avail. The JAR is in the same directory as hworld.java, as well.
Anyone have any idea on how to fix this?
You don't mention whether conio.class is defined in package conio. If it is not, then simply use the class without importing it. Remove the import.
It's actually not possible. You need to put the other class in a package if you want to import it.
What's the syntax to import a class in a default package in Java?
Find out what package Conio is in - an easy way to do this is to open the jar as a zip file, the package will correspond with the folder structure of the archive. For example if Conio is in x/y/z then import x.y.z.Conio and compile/run with conio.jar on the classmate.

Categories