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
Related
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.
I have a program in my desktop that I want to run (structure in the url), but when I do compile, with
\code\nlp\assignments\parsing\javac PCFGParserTester.java
I get:
PCFGParserTester.java:6: error: package nlp.io does not exist
import nlp.io.PennTreebankReader;
^
PCFGParserTester.java:7: error: package nlp.ling does not exist
import nlp.ling.Tree;
^
PCFGParserTester.java:8: error: package nlp.ling does not exist
import nlp.ling.Trees;
^
PCFGParserTester.java:9: error: package nlp.parser does not exist
import nlp.parser.EnglishPennTreebankParseEvaluator;
how do I get my program to correctly import my packages?
You want to be in the \code directory and compile with:
javac nlp\assignments\parsing\PCFGParserTest.java
(And you should have a package declaration of package nlp.assignments.parsing; to match the position in the directory structure.)
That way javac will look for the other classes appropriately.
Alternatively, and rather more simply, you could use an IDE such as Eclipse or NetBeans, and it would take care of all this for you - you'd just specify the code directory as a source directory, and all would be well.
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
I have a file xxx.proto. I downloaded the protobuf compiler and installed it. Then I issued this command
protoc --java_out=./ xxx.proto
and it generated my xxx.java
Now I want to compile this file into a class file which I can use with Scala.
javac xxx.java
Which gives me this error
xxx.java:7: package com.google.protobuf does not exist
com.google.protobuf.ExtensionRegistry registry) {
^
xxx.java:12450: package com.google.protobuf.Descriptors does not exist
private static com.google.protobuf.Descriptors.Descriptor
^
xxx.java:12453: package com.google.protobuf.GeneratedMessage does not exist
com.google.protobuf.GeneratedMessage.FieldAccessorTable
...
...
...
100 errors
Now I guessed, it doesnt have the package.
So I copied the class files of package com.google.protobuf into the same folder where xxx.java exists. Note - I didnt compile this package. I downloaded the jar from another extension which had the jar files. So I extracted them. Now my current path where xxx.java resides has com/google/protobuf/ *.class of protobuf library.
I issued the javac command again.
This time I got a different set of errors -
xxx.java:10: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
^
xxx.java:215: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
^
xxx.java:608: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
^
xxx.java:1017: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
..... 100 errors
I even tried to compile the source files which came with google protobufs. The generated java classes are giving the same errors.
Any ideas what to do ??
Answer
Okay. Thanks everyone.
The main problem is that protocol buffers compiler package from google doesnt by default create the java library. I assumed that it does and installs it. It actually does if you are running Maven. But i didnt have maven
So i compiled the code in /java/src and used the jar.
^
When compiling, you need to have protobuf lib on your classpath. All those missing packages and classes are from protobuf lib.
Find protobuf jar and use
javac -cp path/to/protobuf.jar xxx.java
You may need to use version 2.4.1 (or 2.4+, at least) of the protobuf kit, including making sure that you update protoc (the protobuf compiler) and recompile your proto definition using the new protoc. (In other words, everything has to be the same version:
the protobuf-vn.n.n.jar file;
the protoc compiler; and
the output of compiling your .proto files with protoc.
One I got everything synched, I began to move forward with a Clojure project I'm looking at. You may be encountering the same version skew problem.
protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto
you can download protoc.exe (new release) from>>.
https://code.google.com/p/protobuf/downloads/detail?name=protoc-2.5.0-win32.zip&can=2&q=
in your *.proto file you correctly config
option java_package = "com.example.package";
option java_outer_classname = "class name";
One can install the protobuf jar file using the ubuntu a
apt-get install libprotobuf-java
This will copy the protobuf-java-2.4.1.jar under /usr/share/java/
Hope this helps
I do not have a %CLASSPATH% set up. As I understand, this should not be a problem because Javac will assume a classpath of the current directory.
As you can see below, javac is unable to find my Case class even though it's in the same exact directory. Any thoughts on why this is happening? This code works fine when I use Eclipse.
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>dir /B
Case.class
Case.java
EntryPoint.java
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>javac EntryPoint.java
EntryPoint.java:16: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
ArrayList<Case> cases = new ArrayList<Case>();
^
EntryPoint.java:16: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
ArrayList<Case> cases = new ArrayList<Case>();
^
EntryPoint.java:24: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
cases.add(new Case(new Integer(count), line));
^
3 errors
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>
Update 1:
After trying to compile from my package root (src), I get a new error (even after deleting the Case.class file)
C:\Documents and Settings\joep\My Documents\GCJ\src>javac -cp . codejam2011/Round0/D/EntryPoint.java
codejam2011\Round0\D\EntryPoint.java:16: cannot access codejam2011.Round0.D.Case
bad class file: .\codejam2011\Round0\D\Case.java
file does not contain class codejam2011.Round0.D.Case
Please remove or make sure it appears in the correct subdirectory of the classpath.
ArrayList<Case> cases = new ArrayList<Case>();
^
1 error
C:\Documents and Settings\joep\My Documents\GCJ\src>
Update 2:
It appears to be grabbing the Case.java file from a different package.
C:\Documents and Settings\joep\My Documents\GCJ\src>javac -d ../classes codejam2011\Round0\D\*.java
.\codejam2011\Round0\D\Case.java:4: duplicate class: codejam2011.Round0.C.Case
public class Case
^
codejam2011\Round0\D\EntryPoint.java:16: cannot access codejam2011.Round0.D.Case
bad class file: .\codejam2011\Round0\D\Case.java
file does not contain class codejam2011.Round0.D.Case
Please remove or make sure it appears in the correct subdirectory of the classpath.
ArrayList<Case> cases = new ArrayList<Case>();
^
2 errors
C:\Documents and Settings\joep\My Documents\GCJ\src>
You need to compile from the package root, not from inside the package.
So, cd to the src folder and compile from there.
javac -cp . codejam2011/Round0/D/EntryPoint.java
Update: as per your new problem, you need to recompile Case.java the same way. It was apparently compiled the same wrong way (from inside the package).
If the problem is not yet solved by compiling from the package root directory (see the other answers):
make sure all the source files contain classes with names corresponding to their file name
make sure all the source files contain a package statement corresponding to their position in the source file hierarchy
delete all your .class files before compiling (this should only be necessary once, if you checked everything else).
Thus, if the file is codejam2011\Round0\D\Case.java, it should contain package codejam2011.Round0.D; as the first declaration, and then public class Case { ... }. Also, make sure there is no other source file containing this package and class declaration.
From your error message, it looks like the package statement is package codejam2011.Round0.C; instead (and you also have a class Case in the real codejam2011.Round0.C package).
You are in the wrong directory for compiling.
location: class codejam2011.Round0.D.EntryPoint
That tells me, that your package is codejam2011.Round0.D (which is against the convention (all lowercase) but beside the point ...
cd to the parent dir of codejam2011, which is src, isn't it?
javac codejam2011\Round0\D\EntryPoint.java
might do the trick.
Often you have a directory for compiled classes, like 'bin' or 'classes'. To produce the classes there, use -d (destination):
javac -d ../classes codejam2011\Round0\D\EntryPoint.java
I have similar issue, it might not apply to all cases, but what I have done is remove .gradle, build and out folder and rebuild the program again.