I have SimpleSphere.java and TestClass.java stored in a folder called MyPackage.
Attempting to compile TestClass gives this error:
TestClass.java:7: error: cannot find symbol
SimpleSphere ball = new SimpleSphere(19.1);
^
symbol: class SimpleSphere
location: class TestClass
TestClass.java:7: error: cannot find symbol
SimpleSphere ball = new SimpleSphere(19.1);
^
symbol: class SimpleSphere
location: class TestClass
2 errors
But I am fairly certain I have everything set up correctly (evidently I do not, and yet I remain stubborn!). Also, even if these two files were not part of MyPackage, shouldn't JAVA look in the current directory as default and find SimpleSphere???
Seems that you're compiling the classes directly using javac ClassName.java inside the folder where they are located. You have to move one folder up and compile them since there.
Here's a sample of how the files should be located
- basePath
- MyPackage
+ SimpleSphere.java
+ TestClass.java
In your cmd/shell:
# [basePath] javac MyPackage/SimpleSphere.java
# [basePath] javac MyPackage/TestClass.java
# [basePath] java MyPackage.TestClass
Try moving one folder up and then compiling.
Best of Luck.
Related
Relatively new to Java - I just one to update ONE file inside a JAR file. I've decompiled the JAR, and have access to the Java file.
I am trying to use javac, however when I am using it, it throws errors as it doesn't have references to various dependencies.
Do I need to compile everything to get rid of these? Is there a simple way to just compile the one file?
javac MessageNotificationService.java
...
MessageNotificationService.java:108: error: cannot find symbol
List<EmailNotification> pendingEmails = this.messageDao.findPendingNotification("merchant", "new_message");
^
symbol: class EmailNotification
location: class MessageNotificationService
MessageNotificationService.java:111: error: cannot find symbol
List<Long> ids = pendingEmails.stream().map(EmailNotification::getId).collect(Collectors.toList());
^
symbol: variable EmailNotification
location: class MessageNotificationService
MessageNotificationService.java:182: error: cannot find symbol
for (EmailNotification notification : pendingEmails) {
^
symbol: class EmailNotification
location: class MessageNotificationService
39 errors
Sorry if this is a basic question - all of the search results on this seem to be for self-contained files that don't have errors like this.
As far as I can tell, you just point to the old JAR file
javac -cp "./old-jar-file-1.0.0.jar" ./location-of-file/MessageNotificationService.java
I'm trying to compile the following code (one of two files I need to complete this homework) but I'm getting 2 errors in cmd. This is what cmd throws at me:
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
2 errors
And this is the code I'm trying to compile.
public class CarRentalTest {
public static void main (String[] args)
{
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
myCarRental.Customers();
} //end method main
} //end class CarRentalTest
What's weird is that the whole thing runs fine in NetBeans. What am I doing wrong here? :9
What am I doing wrong here?
Not building CarRental, or not telling the compiler where to find the class if you have already compiled it. The IDE is probably assuming you want to build everything, so that's fine.
We don't know how your code is organized, but you should either pass all the relevant filenames to the compiler at the same time:
javac -d classes src\CarRental.java test\CarRentalTest.java
... or put the output directory of the earlier compilation in the classpath for the later compilation, e.g.
javac -d classes src\CarRental.java
javac -d testclasses -cp classes test\CarRentalTest.java
If you are using a standard directory layout for your project, where production and test code are in separate directory trees then the java command line will not see the production class if your currect directory is the test directory.
To clarify:
Suppose you have this dir structure:
src/
main/
java/
mypackage/
CarRental.java
test/
java/
mypackpage/
CarRentalTest.java
and you are in the 'src/test/java/mypackage/' directory, you would experience this error when running javac at the command line - although the production and test classes are in the same package, they are in different directories.
The IDE knows about this directory structure, includes the test path during compilation and therefore it works OK.
You need to import CarRental class in the CarRentalTest.
import yourpackage.CarRental in the CarRentalTest. Java Compiler can't find the CarRental in the CarRentalTest.java.
In the IDE whole package comes in the java file
import package.car.*;
This is why it is working in IDE.
I created multiple packages and want to compile and run them. I fiddled around with javac and java and learned about how packages should be named and how a project should be structured. I hope I got all right. But I fail at compilation and running the stuff. I know I could use an IDE for this, but I want to try it with the command-line tools just for curiousity.
Here is how my project is organized:
Project
+ src
+ net
+ chris
+ dojo
- Program.java
+ datastructures
- Queue.java
- LinkedList.java
+ sorting
- MergeSort.java
+ bin
+ net
+ chris
+ dojo
- Program.class (should be here but missing because compilation fails)
+ datastructures
- Queue.class
- LinkedList.class
+ sorting
- MergeSort.class
Compilation for the classes in the "datastructures" and "sorting" packages is working fine. Here are the commands I used. The folder structure in the "bin" folder is automatically created by the compiler.
javac -d bin src\net\chris\dojo\datastructures\*.java
javac -d bin src\net\chris\dojo\sorting\*.java
The problem is when I try to compile "Program.java" (thats the test class I run from the command-line) the compiler is throwing errors, because it cannot find the packages "net.chris.dojo.datastructures" and "net.chris.dojo.sorting".
Here is the compilation command:
javac -d bin src\net\chris\dojo\Program.java
This is the output I get:
src\net\chris\dojo\Program.java:3: error: cannot find symbol
import net.chris.dojo.datastructures;
^
symbol: class datastructures
location: package net.chris.dojo
src\net\chris\dojo\Program.java:4: error: cannot find symbol
import net.chris.dojo.sorting;
^
symbol: class sorting
location: package net.chris.dojo
src\net\chris\dojo\Program.java:11: error: cannot find symbol
MergeSort.sort(values);
^
symbol: variable MergeSort
location: class Program
src\net\chris\dojo\Program.java:12: error: cannot find symbol
Queue queue = new Queue();
^
symbol: class Queue
location: class Program
src\net\chris\dojo\Program.java:12: error: cannot find symbol
Queue queue = new Queue();
^
symbol: class Queue
location: class Program
src\net\chris\dojo\Program.java:13: error: cannot find symbol
LinkedList list = new LinkedList();
^
symbol: class LinkedList
location: class Program
src\net\chris\dojo\Program.java:13: error: cannot find symbol
LinkedList list = new LinkedList();
^
symbol: class LinkedList
location: class Program
7 errors
Thats the code of my class files:
Queue.java
package net.chris.dojo.datastructures;
public class Queue {
...
}
LinkedList.java
package net.chris.dojo.datastructures;
public class LinkedList {
...
}
MergeSort.java
package net.chris.dojo.sorting;
public class MergeSort {
...
}
Program.java
package net.chris.dojo;
import net.chris.dojo.datastructures;
import net.chris.dojo.sorting;
public class Program {
public static void main(String[] args) {
int[] values = { 9, 4, 6, 2, 0, 3, 8, 1, 7, 5 };
MergeSort.sort(values);
Queue queue = new Queue();
LinkedList list = new LinkedList();
}
}
I would run it with this command:
java -cp bin net.chris.dojo.Program
I execute all commands in the root folder of the project.
Thanks for your help.
The solution was to include the classpath when compiling. That way it can find the packages it depends on.
javac -d bin -cp bin src\net\chris\dojo\Program.java
Thanks #BigMike for the solution.
Try change this in your Program class
import net.chris.dojo.datastructures;
import net.chris.dojo.sorting;
to
import net.chris.dojo.datastructures.*;
import net.chris.dojo.sorting.*;
And when you compile your Program.java use following command
javac -d bin src\net\chris\dojo\Program.java -classpath bin
So, I'm actually coding primarily in python, but I need to do one thing in java, and having no java experience, I'm really not understanding how this works.
My program creates java files, then compiles them with command prompt, and puts them into the minecraft.jar, but I don't really understand what I need to write in command prompt to set the classpath to the minecraft.jar. Especially because I need this to work on everyone's computer, and I won't know where my program (and thus the java files) are.
Could anyone give me a hand with this?
Edit:
Okay, so I tried doing this:
javac -classpath %appdata%\.minecraft\bin\minecraft.jar BLOCK1.java
And it gives me this error:
LLBLOCK1.java:3: error: cannot find symbol
public class LLBLOCK1 extends Block
^
symbol: class Block
LLBLOCK1.java:5: error: cannot find symbol
private World worldObj;
^
symbol: class World
location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
^
symbol: class World
location: class LLBLOCK1
LLBLOCK1.java:12: error: cannot find symbol
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
^
symbol: class EntityPlayer
location: class LLBLOCK1
LLBLOCK1.java:9: error: cannot find symbol
super(i, j, Material.wood);
^
symbol: variable Material
location: class LLBLOCK1
LLBLOCK1.java:14: error: package Block does not exist
world.setBlockWithNotify(i + 0, j + 0, k + 0, Block.stone.blockID);
^
6 errors
Am I writing the classpath wrong?
the minecraft.jar is almost always located in
C:\Users\User_Name\AppData\Roaming\.minecraft\minecraft.jar
In batch language you can use:
%appdata%/.minecraft/minecraft.jar
However you may want to also back it up first, copy and rename the minecraft.jar so if anything goes horibly wrong you can always restore it easier.
If you are compiling the jar, you can type in the location of the output for the jar:
-jar cvfm c:/users/matt/documents/Minecraft.jar manifest.txt *.class
I believe this would work if you were to create your own jar. This would be easier then directly editing the Minecraft jar.
My experience with Java: Read-Only
I have these lines in my code:
import com.altova.io.*;
import com.mapforce.*;
The packages are in Mapping.jar, which is on my classpath, indeed it is first. Javac -verbose admits this:
[search path for source files: Mapping.jar,.,[etc]
When the compiler gets to the use lines, however:
[loading com/altova/io/Input.class(com/altova/io:Input.class)]
ShapeTypeFiddle.java:339: cannot find symbol
symbol : class io
location: package com.altova
com.altova.io.StringInput(sthing.toString());
^
(+ two others, one is the MappingMapToinput2Output.run(input, output), the other is the output.getContent() call.)
Unzipping the Mapping.jar file does show compiled .class files for the Input.class, the MappingMapToinput2Output.class and the Output.class class files.
What else can I check?
It's looking for the class io which should have the static method StringInput. But you actually want to create an instance of StringInput. That can only mean that you forgot the new operator.
new com.altova.io.StringInput(sthing.toString());