So I was following this tutorial:
https://spring.io/guides/gs/maven/
I cloned their repositories for the software for fear of mistyping something.
The code does not work when I compile Greeter.java using javac and then use java to run HelloWorld.java file It gives me the following error:
HelloWorld.java:5: error: cannot find symbol
Greeter greeter = new Greeter();
^
symbol: class Greeter
location: class HelloWorld
HelloWorld.java:5: error: cannot find symbol
Greeter greeter = new Greeter();
^
symbol: class Greeter
location: class HelloWorld
2 errors
I tried explicitly importing Greeter into HelloWorld using
ìmport hello.Greeter
The code works fine when I run it without the package hello;statements.
Any idea why I am getting this error??
So I followed through with the tutorial and using mvn package command and the jar file generated the project works.
So is this issue with trying to compile it with java command in the command line.
Adding directory structure of the project
pom.xml src target
./src:
main
./src/main:
java
./src/main/java:
hello
./src/main/java/hello:
Greeter.java HelloWorld.java
I assume you try to compile the sources while you're in the directory src/test/java/hello. That's the wrong directory, you have to do it from directory src/test/java and pass the directory (i.e. package) to the compiler, e.g.
javac hello/*.java
Another reason might be that you haven't compiled Greeter.java, so the compiler doesn't find the class-file while compiling Hello.java. Above command should solve that.
If you have a main method in hello run
java hello/name-of-file.java
to run your main method.
Related
I am trying to migrate from IDEs like Eclipse to a standalone Java environment, but I'm having problems tying together multiple files into a project.
Here is some sample code, where both files are in the same directory:
App.java
package com.example.main;
public class App {
public static void main(String[] args) {
Example.test();
}
}
Example.java
package com.example.main;
public class Example {
public static void test() {
System.out.println("It's working");
}
}
When running App.java in an IDE, the expected output of It's working is printed, however after executing javac *.java the files seem to ignore eachother.
Here is the error that occurs when executing java App.java after it's been compiled:
App.java:5: error: cannot find symbol
Example.test();
^
symbol: variable Example
location: class App
1 error
error: compilation failed
How can I compile the files in a project so that they recognise eachother?
If you running Java 11 and above, java App.java will compile App.java only.
If you need to refer Example.java, first you need to compile both java files into a directory.
Let give it named 'classes'. The command will be
javac -d classes *.java
After that, you can run it via
java -cp classes com.example.main.App. Please note that App is without .class suffix
Of course, it is advisable to use build tools like Apache Maven or Gradle to build your project if it grow larger or need other dependencies.
I am trying to compile java classes using javac. The project uses ByteInputStream class that is located in java_version/jre/lib/rt.jar file(in package com.sun.xml.internal.messaging.saaj.util). javac compilter complains that can't find that class. I tried to include all library classes on during compilation(lib1:lib2:lib3:etc) but still no result.
Here is error message:
package com.sun.xml.internal.messaging.saaj.util does not exist
cannot find symbol : class ByteInputStream
Any ideas?
I'm trying to write a simple ant build to compile a project.
The project is in eclipse and there it compiles successfully (with the eclipse-compiler).
But with ant (using javac) it appears an error and i don't know how to resolve it.
Structure of the used jar:
com
xxx
a <= package
b
a.class
Codeblock of my class:
Object o = com.xxx.a.b.method();
^
The exception of ant is:
error: cannot find symbol
symbol: variable b
location: class a
I think eclipse uses the package first to try to compile the code. javac seems to think that a is the class.
Is there a way to resolve the problem without changing the jar?
It looks like either package name is different or you have multiple class files of the same name. I would suggest checking the import statements and adding the specific jar file to classpath while compiling using javac or ant command.
To find the exact jar file, use ctrl+T then paste your class name in the box and it will tell you the jar file. Add that jar file to your ant classpath and build.
I didn't find anything in the Java Language Specification that this is an error, so it might be a javac bug.
Since it is a javac vs. Eclipse compiler thing, try one of the following:
Use the Eclipse compiler in the Ant script
If it is a javac bug, the bug may be fixed in a newer (update) JDK version
If your code does not directly reference class com.xxx.a, compile the code with the JAR in which the class com.xxx.a was removed
I am learning how to create an RMI system, and I compiled the shared classes into a .jar, but when I try to include it, the following error occurs:
javac -cp compute.jar Client.java
Client.java:6: error: package rmi.interfaces does not exist
import rmi.interfaces.Tasks;
^
Client.java:17: error: cannot find symbol
Tasks stub = (Tasks) registry.lookup("Tasks");
^
symbol: class Tasks
location: class Client
Client.java:17: error: cannot find symbol
Tasks stub = (Tasks) registry.lookup("Tasks");
^
symbol: class Tasks
location: class Client
3 errors
The classes it needs are inside the jar, but for some reason it can't find them. How can I fix this?
The message says that rmi.interfaces package is not on your classpath. In you example this means not in compute.jar. You could verify that by typing zipinfo -1 compute.jar to see what entries are inside the JAR.
You really should setup a build tool for your project. You really shouldn't be compiling Java by hand unless you are learning what is javac. If you want to try Maven you can clone rm5248/Java-RMI-Example to see how to set up an RMI project with independent client and server modules.
I'm new in Android SDK. I'm using Emacs as IDE (I know about Android Studio). I created a project and I compiled it from a script named gradlew (It was created automatically when the project was created). APK is created successfully. Now, I'm trying to implement (with FlyMake) syntax error checker. The command used to do this is the following:
javac "main.java"
where main.java is the main of the application (there is only one file).
Obviously, javac doesn't know where is the SDK (API level 20). So I tell it as follow:
javac -sourcepath "~/opt/android/sources/android-20" "main.java"
but it throws a lot of errors like "class not found". For example:
/home/carlos/opt/android/sources/android-20/android/app/Activity.java:29: error: cannot find symbol
import android.content.IIntentSender;
^
symbol: class IIntentSender
location: package android.content
When I see the content of android/content, there is not a .java named IIntentSender
So, what's happening? Thanks for reading and answering!
This question was answered by CommonsWare:
The last time I used javac by hand, I used -classpath, not -sourcepath. The appropriate JAR for -classpath would be ~/opt/android/platforms/android-20/android.jar, assuming that ~/opt/android/ is where your Android SDK is installed. IIntentSender is generated Java source code (from AIDL) and will not be in Java form in the SDK sources directory. So, if FlyMake requires -sourcepath, I expect that you're going to be in a world of hurt.
Thanks!