I am trying to import a package I created, my file structure is as follows:
javacode
tester.java
mypackage
Cram.class
In my tester I import the package using: import mypackage.Cram;
but when I try to compile my tester.java I get the following
error: package mypackage does not exist. Any help would be much appreciated. Here is the error; algo is my tester.java and pack is mypackage.
algo.java:6: error: package pack does not exist
import pack.Cram;
^
If you are compiling all of your files at the same time, then you should just be able to use your code as-is, provided that you have your classes defined properly and in the directory structure as indicated by your package keyword.
Suppose you have the following directory tree (as in your original post):
javacode
Tester.java
mypackage
Cram.java
classes
<.class files will be placed here later>
And the following classes defined:
Tester.java:
import mypackage.Cram;
public class Tester {
public static void main(String[] args) {
Cram c = new Cram();
c.doSomething();
}
}
Cram.java:
package mypackage;
public class Cram {
public void doSomething() {
System.out.println("Hello from Cram!");
}
}
You can compile all of these files into a single directory (usually, IDE's will compile to a "classes" or "bin" directory) with the following command:
> javac -d classes Tester.java mypackage\Cram.java
This will place all your class files in the directory "classes"
classes
Tester.class
mypackage
Cram.class
You can then run by using:
> java -cp classes Tester
And produce output:
Hello from Cram!
If you are compiling your package separately from Tester.java, like a library, then you can do the same thing, just with some separate commands.
Compile mypackage.Cram:
> javac -d classes mypackage/Cram.java
This will put the .class files in the same classes directory. When you try to compile Tester.java (which uses mypackage.Cram), you simply tell the compiler where your classes are:
> javac -d classes Tester.java
and then run:
> java -cp classes Tester
produces the same output.
My guess is, your classes and names are all mangled, and Java expects them to follow a convention.
When you compile, it is looking for .java files. Put the Cram.java file in there, and try compiling again.
Change your import in Algo.java. You are looking for pack.Cram, and you have indicated it is mypackage.Cram.
Related
Trying to compile a multi file package. Needing to compile via: javac mainfile.java while also compiling all the other (about 4 other .java files) at the same time through the one file? I have tried using statements like extend and import package.* Any help would be appreciated.
I have used different compiling methods and arguments but trying to do it just by only inserting javac mainfile.java and java mainfile
Summary: Trying to compile multiple java files at once, through java compiling a single file.
My current code for the main file:
package mypackage;
import mypackage.*;
public class mainfile{
public static void main(String[] args) {
Myfile.main(args) //run main from other file
}
}
Edit: Sorry for the lack of information, when compiling, the compiler returns:
MainFile.java:15: error: cannot find symbol
Myfile.main(args);
^
symbol: variable Myfile
location: class mainfile
1 error
javac as a tool does not do what you want. But, that's why other tools exist.
What you're asking for boils down to 'I want a build system'. The vast majority of java projects use maven or gradle.
It would seem like this works fine:
javac -sourcepath src src/mainfile.java
but you'd be deceived. That will merely compile all source files that are directly referenced by the code in mainfile.java, but there are many other ways to refer to code, such as SPI, reflection, XML config files. The vast majority of java projects will end up using some construct that ends up 'breaking' the -sourcepath "trick" sooner rather than later, which is presumably why all java projects use a build system instead of relying on -sourcepath.
Note that all source files act as if they have:
import java.lang.*;
import yourownpackage.*;
at the top, whether you write this out or not. And, import is java-ese for 'alias'. import foo.bar.Baz; means: Whenever 'Baz' appears in this file as a type, assume I meant to write 'foo.bar.Baz', and that is all: import foo.bar.Baz does not run any code that is in the Baz class whatsoever. If you don't use Baz, then the class file produced doesn't mention Baz at all. This will also not cause javac to then compile your entire directory.
I've used Eclipse for most of my Java programming, but I'm now volunteering at a prison to teach inmates, and they don't have access to any IDE so we have to do everything from the terminal. (I'm also primarily a Math teacher but doing what I can for the CompSci stuff.) As I understand the file structure in Eclipse, when you have a package called, say, fsk, you place it in a hierarchy like
fsk
src
Sort.java
Main.java
bin
Sort.class
Main.class
where the Main class calls on the Sort class, so to compile the source code at the terminal I write
$~/fsk/src: javac Sort.java -d ../bin
The Sort.java file starts with
package fsk;
...
and the Main.java file has the package name and makes a simple call to the Sort class
package fsk;
import java.util.List;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
List<Integer> li = new LinkedList<Integer>();
li.add(1);
Sort<Integer> s = new Sort<Integer>();
s.insertionSort(li);
}
}
The Sort.java file seems to compile just fine, but when I try to compile the Main.java file I get the error that it cannot find the symbol "Sort".
If I do all of this without any package declaration, everything works just fine. Besides modeling this after how I understood Eclipse to work I have also been using https://docs.oracle.com/javase/tutorial/java/package/managingfiles.html and thought I was faithfully reproducing the structure and code, but apparently not.
[Edit: Correction, I have been mostly going off of the Eclipse structure as I understand it, I'm now noticing that when the Oracle tutorial structures this it uses a structure like
src
fsk
Sort.java
Main.java
bin
fsk
Sort.class
Main.class
So I suppose my question now is: am I misunderstanding how Eclipse structures the files, and do I need to instead structure them as in the above?]
The structure is fine. You'll want to compile all the Java files with one javac invocation.
javac fsk/*.java -d ../bin
This will get annoying once you start having multiple packages. Long term you should use a build tool such as Maven, Ant, or Make. Compiling files by hand gets mighty tiresome.
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.
Good morning,
I have the file Test.java that is in /home
class Test {
public static void main(String[] args) {
prog.io.ConsoleOutputManager out=new prog.io.ConsoleOutputManager();
pack1.A a=new pack1.A();
out.println(a.toString());
}
}
that gives me the error:
Test.java:3: error: package prog.io does not exist
prog.io.ConsoleOutputManager out=new prog.io.ConsoleOutputManager();
^
Test.java:3: error: package prog.io does not exist
prog.io.ConsoleOutputManager out=new prog.io.ConsoleOutputManager();
^
Test.java:5: error: package pack1 does not exist
pack1.A a=new pack1.A();
^
Test.java:5: error: package pack1 does not exist
pack1.A a=new pack1.A();
^
4 errors
pack1, pack2 and prog.io are in /home as well..Why it doesn't find them?
It is not sufficient to have the class files in the same directory. They must have the same package declaration as well.
Additionally /home must be part of your Classpath. If /home is your Classpath the files must be in subdirectories of /home:
/home/prog/io
ConsoleOutputManager.class
/home/pack1
A.class
/home
Test.class
ConsoleOutputManager.java should have a declaration like package prog.io; before any import or class statements. A.java should have package pack1;.
With that, you can run your class informing the classpath using:
java -cp /home Test
And it will find its dependencies.
You can also place your dependencies in a JAR file with that same structure, and then include the JAR file in your classpath as well:
java -cp yourJar.jar:/home Test
Please make sure that you have set the classpath such that it can find the packages that you are using in your code.
Check if you have package declaration in your ConsoleOutputManager class as well. It should be:
package prog.io;
as the first line in your file.
Your source files are not organised into packages properly or the packages are missing. Your class file defines
prog.io.ConsoleOutputManager out=new prog.io.ConsoleOutputManager();
pack1.A a=new pack1.A();
so, JVM will look for class ConsoleOutputManager in prog.io package and class A in pack1 package and couldn't be found and throws package doesn't exist error. The solution is to add the missing packages sources in build path and keep your source files organised
I am trying to use pdfbox to write a simple pdf file but the problem is that I am getting error :
cannot find symbol class PDDocument
I have downloaded the jar files into the same folder the program exists. How to fix this compilation error?
package org.apache.pdfbox.pdmodel.PDDocument;
import java.io.*;
import org.apache.pdfbox.pdmodel.PDDocument;
public class pdf
{
public static void main(String args[])
{
}
}
Putting the jar in the same folder or package does not add it to the class path. You need to mention the path of the jar in your class path while running your java program. Here is the syntax for that:
To compile:
javac -classpath .;yourjar.jar src/your/package/*.java
To run
java -classpath .;yourjar.jar src/your/package/yourprogrammeclassname
You will need to make sure that the JAR file is on the classpath.
having a similar issue I found that I did not have the correct syntax on the import line in the java source
doing a compile as the following (on windows):
javac -cp .;commons-io-2.4.jar AgeFileFilterTest.java
with commons-io-2.4.jar in the same folder as AgeFileFilterTest.java
I was getting error:
import org.apache.*;
^
AgeFileFilterTest.java:24: error: cannot find symbol
displayFiles(directory, new AgeFileFilter(cutoffDate));
^
It was puzzling becuase it seemed all was in place; the jar was in the folder, defined in the classpath, and upon jar content inspection I could see what was being referenced- using 7zip I opened the jar file and could see:
commons-io-2.4.jar\org\apache\commons\io\filefilter\AbstractFileFilter.class
I then read in some post "you do not import the class" which got me to think about the import syntax...
I changed it from:
import org.apache.*;
changing it to:
import org.apache.commons.io.filefilter.*;
and wala compile error gone using:
javac -cp .;commons-io-2.4.jar AgeFileFilterTest.java
and program worked using
java -cp .;commons-io-2.4.jar AgeFileFilterTest