Can't find a solution to this problem.
Guitar2.java:
public class Guitar2
{
private String serialNumber;
public Guitar2(String serialNumber)
{
this.serialNumber = serialNumber;
}
public String getSerialNumber()
{
return serialNumber;
}
}
Inv2.java:
import java.util.List;
import java.util.LinkedList;
public class Inv2
{
private List guitars;
public Inv2()
{
guitars = new LinkedList();
}
public void addGuitar(String serialNumber)
{
Guitar2 guitar = new Guitar2(serialNumber);
guitars.add(guitar);
}
}
Both files are in the same directory, both are 755 and the directory is in the classpath. I get the error message:
[machine]me # directory $ javac Inventory.java
Inventory.java:18: cannot find symbol
symbol : class Guitar
location: class Inventory
Guitar guitar = new Guitar();
^
Inventory.java:18: cannot find symbol
symbol : class Guitar
location: class Inventory
Guitar guitar = new Guitar();
^
2 errors
I read that if a file is in the same directory, classes from it can be used in other files in the same directory without any import statements. What's the problem here?
POST EDIT Output when using the above code:
[me]machine # ricks $ javac Inv2.java
Note: Inv2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
I get the .class files of both .java files.
Run javac Guitar.java
and only then (after it has been compiled and a Guitar.class was created) run javac Inventory.java
Try to compile Guitar.java first. then run your cmd . or try this : javac *.java
Make sure both classes have the same package. Even if they are in the same directory, a different package could cause this problem. Type the same package or use an import and then try compiling with javac *.java
Related
I am trying to re-learn some programming skills.
I have decided to use an old favourite - Logic Gates Simulation - as a learning tool.
I want to set this project up to use packages.
My CLASSPATH is "C:\Users\ruthm\Documents\java"
My project code is in the directory:
C:\Users\ruthm\Documents\java\logic
I am using Java 11
My classes so far are Connector.java and ConnectorTest.java
the code for Connector is as follows:
package logic;
/*
Class Connector
A connector forms the input and output of a LogicGate.
The connector "output" object of a gate can be passed to another gate to form the input.
*/
public class Connector{
private int value;
public Connector(){
value=0;
}
public Connector(int state){
value=state;
}
public void setValue(int state){
value=state;
}
public int getValue(){
return value;
}
}
The code for Connector Test is as follows:
/* Test Case for Connector */
import logic.*;
class ConnectorTest{
public static void main (String[] args){
logic.Connector myConnector = new logic.Connector();
System.out.println("initial value: "+myConnector.getValue());
myConnector.setValue(1);
System.out.println("Set value: "+myConnector.getValue());
}
}
Connector.java compiles without error.
When I try to compile ConnectorTest.java I get the following from the compiler:
C:\Users\ruthm\Documents\java\logic>javac ConnectorTest.java
ConnectorTest.java:4: error: package logic does not exist
import logic.*;
^
ConnectorTest.java:9: error: package logic does not exist
logic.Connector myConnector = new logic.Connector();
^
ConnectorTest.java:9: error: package logic does not exist
logic.Connector myConnector = new logic.Connector();
^
3 errors
C:\Users\ruthm\Documents\java\logic>
I have been following guides on directory structure and packages to try and solve this but I am clearly not understanding something.
I get the same errors if I declare ConnectorTest to be in package logic as well.
Can someone handhold me and show me where I am going wrong?
My project code is in the directory:
C:\Users\ruthm\Documents\java\logic
This means that to avoid trouble, you'd be better off using same package directive to them both.
So if you add this:
package logic;
to your ConnectorTest class, it should be ok, given you don't have any other issues.
If you want to keep your ConnectorTest class in default package, you could move your ConnectorTest.java file to C:\Users\ruthm\Documents\java directory, but keep your Connector class in the logic directory.
Good morning, everybody.
So, I'm trying to compile a very super-simple Java program by way of a package structure, in which I have two java files. My folder structure is:
Prac/Java/LL/people/src/coreservlets/
and the two files are PersonTest.java (which is the main one) and Person.java
The PersonTest.java file goes as follows:
package coreservlets;
public class PersonTest {
public static void main (String[] args) {
Person p = new Person("Jane", "Smith");
}
}
and Person.java goes as follows:
package coreservlets;
public class Person {
public String firstName, lastName;
public Person(String initialFirstName,
String initialLastName) {
this.firstName = initialFirstName;
this.lastName = initialLastName;
}
public String getFullName() {
return(firstName + " " + lastName);
}
}
And when I try to compile, it tells me that it can't find the symbol Person. I've checked multiple forum entries to check to make sure I'm doing package structure correctly, and I can't figure out what the problem is. What am I doing wrong?
The issue was that the class path needs to be set for each command (javac and java):
Follow bellow Steps
instead of going to subpackage, compile Person.java from the top_level:
$javac -cp . Prac/Java/LL/people/src/coreservlets/Person.java
compile PersonTest.java in the same way:
$javac -cp . Prac/Java/LL/people/src/coreservlets/PersonTest.java
run the file using the class path also:
$java -cp . Prac/Java/LL/people/src/coreservlets/PersonTest
ref : http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
I have two class like so:
package one;
public class Parent
{
protected String name = "hello world";
}
And:
package two;
import one.Parent;
class Child extends Parent
{
public void testMethod()
{
System.out.println("name is " + name);
}
public static void main(String args[])
{
Child n = new Child();
n.testMethod();
}
}
This classes are in the c\source folder
When I compile this classes in command line I get lot's of error I use more statement like so:
C:\source>javac -d Child.java
c:\sorce> javac *.java
Still I get many errors.
How do I solve this problem?
I do a simple test. you can try again
the current directory structure.
--file
--bin
--Parent.java
--Child.java
then execute the following cmd
cd bin
javac -d ./ ../Parent.java
javac -d ./ ../Child.java
java two.Child
The result is ok
if your code has a package name, you should use the option -d, in the compile process, javac will create directories according to the package name.
for javac tool
please refer
http://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/win32/javac.html
help that helped.
This is my directory layout:
~ koraytugay$ ls -1 biz/tugay/hellospring/
Bike.java
Car.java
Vehicle.java
VehicleApp.java
VehicleService.java
beans.xml
So I am in the root folder and my .java files are in biz/tugay/hellospring/
Code for VehicleApp:
package biz.tugay.hellospring;
/* User: koray#tugay.biz Date: 29/06/15 Time: 15:16 */
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class VehicleApp {
public static void main(String[] args) {
ApplicationContext applicationContext
= new ClassPathXmlApplicationContext("biz/tugay/hellospring/beans.xml");
VehicleService vehicleService = (VehicleService) applicationContext.getBean("vehicleService");
vehicleService.driver();
}
}
and VehicleService:
package biz.tugay.hellospring;
/* User: koray#tugay.biz Date: 29/06/15 Time: 15:10 */
public class VehicleService {
private Vehicle vehicle;
public void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
}
public void driver(){
System.out.println(vehicle.drive());
}
}
Also in my home directory I have the following jar files:
~ koraytugay$ ls -1 *.jar
spring-aop-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-security-core-3.2.5.RELEASE.jar
I tried several variations however I was not successful with any of them. One example:
~ koraytugay$ javac -cp .:/biz/tugay/hellospring biz/tugay/hellospring/VehicleApp.java
biz/tugay/hellospring/VehicleApp.java:4: error: package org.springframework.context does not exist
import org.springframework.context.ApplicationContext;
^
biz/tugay/hellospring/VehicleApp.java:5: error: package org.springframework.context.support does not exist
import org.springframework.context.support.ClassPathXmlApplicationContext;
^
biz/tugay/hellospring/VehicleApp.java:10: error: cannot find symbol
ApplicationContext applicationContext
^
symbol: class ApplicationContext
location: class VehicleApp
biz/tugay/hellospring/VehicleApp.java:11: error: cannot find symbol
= new ClassPathXmlApplicationContext("biz/tugay/hellospring/beans.xml");
^
symbol: class ClassPathXmlApplicationContext
location: class VehicleApp
4 errors
I have the .jar files in the folder I am executing javac. Why the compiler is unable to find ClassPathXmlApplicationContext ?
you either need to specify every jar
java -cp ./spring-aop-3.2.5.RELEASE.jar ; ./spring-beans-3.2.5.RELEASE.jar ;./spring-context-3.2.5.RELEASE.jar ; ./spring-security-core-3.2.5.RELEASE.jar
or use a wild card
java -cp *.jar; /otherpath
but thats only works with java 6 up
I have in one folder /projects/A/Test.java
with the following code:
public class Test {
public static void t() {
System.out.println("Hey");
}
}
and in folder#2 /projects/
with the following code:
public class Test2 {
public final static void main(String[] args) {
Test t = new Test();
t.t();
}
}
I compile the first one with: javac Test.java
I compile the second one with javac Test2.java -cp ./A
Now when I try to run Test2: java Test2, I receive an error:
Exception in thread "main" java.lang.NoClassDefFoundError: Test
How can I run Test2, which calls Test from a subfolder?
If your classes are in different places you have to set the classpath when running, just like when you compile.
java -cp .:./A Test2 # On Unix use :, on Windows ;
The problem is that in class test you had to specify package in the beginning of the file.
package A;
And then import it in your Test2 class.
import A.Test
you don't have to set package and import Test if both locations project and project/A are in your classpath