Java package and/or file structure compiling issue - java

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

Related

How Do I Import Classes From Another Package and Folder in Java?

I just started studying OOP and Packages in Java. I have a question regarding package importing in Java.
I have two files, named ImportThis.java and Here.java
The directory for ImportThis.java on my local machine is F:\VS Codes\master\folderone\folderoneone\ImportThis.java. And the contents of ImportThis.java is:
package master.folderone.folderoneone;
public class ImportThis {
public static void aStaticMethod() {
System.out.println("Hello World");
}
}
The directory for Here.java on my local machine is F:\VS Codes\master\foldertwo\foldertwotwo\Here.java. And the contents of Here.java is:
package master.foldertwo.foldertwotwo;
public class Here {
public static void anotherMethod() {
ImportThis.aStaticMethod();
}
}
By looking at the contents of Here.java, you might be able to tell that I want to import the class ImportThis from ImportThis.java to Here.java, and it is indeed what I've been trying to do. But both ImportThis.java and Here.java came from different folders and packages. I've tried using import master.folderone.folderoneone.ImportThis; on Here.java but VS Code says it cannot be resolved. Looking forward to the answer for my question!
EDIT: Changed package names and lowercased the folder names
Try:
package master.foldertwo.foldertwotwo;
import master.folderone.folderoneone.ImportThis; //<-import statement
public class Here {
public static void anotherMethod() {
ImportThis.aStaticMethod();
}
}

Run Package base program in command line

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.

package, class and excecutionals files structure

In my Win7 machine I have added in the CLASSPATH like this:
CLASSPATH=D:\Dev\Java;C:\Program Files (x86)\Java\jre1.8.0_20\lib\ext\QTJava.zip.
In my directory tree I have created a D:\Dev\Java\abc folder and placed a filed called Address.java that contained this code:
package jme;
public class NewClass {
}
Having done that, I created a project that looks like this:
package javaapplication1;
package abc; // << Error
public class JavaApplication1 {
public static void main(String[] args) {
abc.Address address; // << Error
System.out.println("Jaaaa");
}
}
Why the abc package, when located in the CLASSPATH, is not recognized?
You need to use import ...
package javaapplication1;
import abc.*; // No error if you have the package in the classpath ...
public class JavaApplication1 {
public static void main(String[] args) {
Address address; // No need to prefix with abc, since you imported it before ...
System.out.println("Jaaaa");
}
}
You can't declare double package for a class in Java, and I think that is not what you really want to do ...
To import correctly the classes contained in the abc package make sure to have the abc package and their related classes in your classpath ...
Sorry guys for the horrendous mishap, I am kinda new here, but I'm a quick learner.
The CLASSPATH reads: D:\Dev\Java\abc;C:\Program Files (x86)\Java\jre1.8.0_20\lib\ext\QTJava.zip

Can't use a Java class in another file

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

Is it possible to import class file with default package?

I have created a project and Hello.java file without package name
public class Hello {
public String sayHello(){
return "Say Hello";
}
}
I exported this project into hello.jar.
Now I have created another project and Main.java to call sayHello() method. I added hello.jar in classpath but the below code showing me an error 'Hello cannot be resolved to a type'
public class Main {
public static void main(String[] args){
Hello h=new Hello(); // Error
}
}
It's not possible due to the fact that your Hello.java class needs to be inside a package stored in your JAR file to enable you to reference it. The structure of your JAR should at least be
hello.jar/packageName/Hello.java
after creating it like that it will be imported as
import packageName;
and you will be able to use classes from aformentioned package.
No. It is not possible.
Try giving package name to Hello java file and then create again jar and then write import statement in Main class file
package com;
public class Hello {
public String sayHello(){
return "Say Hello";
}
}
and in Main class add following line
import com.Hello;
public class Main {
public static void main(String[] args){
Hello h=new Hello();
}
}

Categories

Resources