Getting Netbeans Java program to compile - java

I'm new to java, and I've been trying to get my program to compile using Netbeans. HelloWorldApp.java uses the Greeter class in Greeter.java. I keep getting errors and I can't figure it out. I understand that you have to include "packages" or something. I don't have a lot of experience with Netbeans either. But I would love for this to work.
Here is the HelloWorldApp.java:
package helloworldapp;
import Greeter
public class HelloWorldApp
{
public static void main(String[] args)
{
Greeter myGreeterObject = new Greeter();
myGreeterObject.sayHello();
}
}
And here is Greeter.java:
public class Greeter
{
public void sayHello()
{
System.out.println("Hello, World!");
}
}

Change the first line of Greeter to
package helloworldapp;
And then remove
import Greeter
from HelloWorldApp. You only need to import classes that are in other packages. Also, an import line is terminated with a semicolon. Finally, import is always optional and a convenience for the developer; as an example,
import java.util.Calendar;
Allows you to write
Calendar cal = Calendar.getInstance();
But, without the import you could still use
java.util.Calendar cal = java.util.Calendar.getInstance();

Just put the Greeter class in the same folder (i.e. package) as the other file and remove the "import Greeter" statement. You should put every class in a package as you did with the HelloWorldApp class.
If you leave classes without package (i.e. in the root folder) you cannot import them.

As long as both are in the same package (folder) there will be no need for the "import Greeter" statement, this should fix it, hope this helps!

Related

Shortcut to remove package references from code body into import list in eclipse?

Is it possible to remove package references from the code body and put it in the import list using a shortcut? Example
Before:
package com.path.to.SomeClass;
public class SomeClass {
private com.path.to.a.random.OtherClass otherClass;
}
After:
package com.path.to.SomeClass;
import com.path.to.a.random.OtherClass;
public class SomeClass {
private OtherClass otherClass;
}
I know we can do this manually, but I have a file with a lot of these buggers so if anyone knows of a shortcut that can automatically do this I would be very grateful!

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();
}
}

Netbeans doesnt recognize class in the same package

i am creating a little game with libgdx framework and netbeans 8. I have all java classes in a single package that match with the directory structure.
The problem is that i cant import or isntantiate classes, for example:
package com.myfolder.folder2;
import ...
public class myclass1{
private myclass2 mc2;
etc...
}
In this case myclass2 is public and is inside the package but netbeans complains "cannot find symbol".
If i try with alt+enter, netbeans says "Create class myclass2 in package com.myfolder.folder2" or the same like an inner class. If i press the first option, netbeans create a class in the package with the file name myclass2_1 (becouse myclass2 exists!), and myclass1 doesnt recognize the new class.
If i try to import the class:
import com.myfolder.folder2.myclass2;
It gives me the same error, and in fact the code completion tool only gives me one crazy option in the import sentence:
import com.myfolder.folder2.myclass1;
Import the same class.
What can i do? I never have these problems using netbeans.
PD: Sorry for my english :)
You can use a class inside the same package like this:
ClassName classVariableName = new ClassName();
Then when you want to run something from the class you would put
classVariableName.MethodThatIWantToRun();
Or if you want to access a property from that method you would access it in a very similar way:
classVarabileName.PropertyIWantToAccess
Example:
You have one class with a property you want to access:
class MyClass {
public int MyProperty = 5;
}
You access it in this class:
public class MainClass {
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.MyProperty);
}
}
If that doesn't work than you might have some other problem.
It was an error with one of my class package definition:
public class DesktopLauncher{
public static void main(String... args){
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
.
.
.
new LwjglApplication(new MyClass, config);
}
}
It was in MyClass, becouse i copied a snippet from an older project, and accidentally copied the older package.
NetBeans is not smart enough,
Solution: just declare the package name in all classes, example:
Class A:
package test;
public class ClassA {
public static void main(String[ ] args) {
ClassB.myFunctionB();
}
}
Class B:
package test;
public class ClassB {
public static void myFunctionB () {
System.out.print("I am ClassB!");
}
}

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

How do I reference a jar from an eclipse project?

I have some code copied from the fitness website:
package fixtures;
import static fitnesse.util.ListUtility.list; //fitnesse.util can not be resolved
import static util.ListUtility.list; //this one resolves but is no help in getting list to work below
import java.util.Date;
import java.util.List;
public class EmployeesHiredBefore {
private Date date;
public EmployeesHiredBefore(Date date) {
this.date = date;
}
public void table(List<List<String>> table) {
//optional function
}
public List<Object> query() {
return
list(
list(
list("employee number", "1429"),
list("first name", "Bob"),
list("last name", "Martin"),
list("hire date", "10-Oct-1974")
),
list(
list("employee number", "8832"),
list("first name", "James"),
list("last name", "Grenning"),
list("hire date", "15-Dec-1979")
)
);
}
}
I have added to build path with add external jar of fitnesse.jar its contents includes util/ListUtility.class
Anyone know how to reference this jar?
The current version of fitnesse.jar (from http://fitnesse.org/FrontPage.FitNesseDevelopment.DownLoad) has a util.ListUtility class but not fitnesse.util.ListUtility.
So you (probably) want
import static util.ListUtility.list;
but what problem are you actually getting when you use this import? In what way do things not work?
In the fitnesse jar there is no package called "fitnesse.util".
Recheck.
I could find only
util.ListUtility.list;
I am using fitnesse version - release 20150114.
There is no ListUtility under following:
Util package
fitnesse.util package
I resolved it by creating ListUtility class in source by followng way.
Fitnesse importing util.ListUtility.list; is giving error

Categories

Resources