So I've tried to start compiling and running java on sublime, and it works fine if the package is not defined.
this compile and run:
public class Tester
{
public static void main(String[] args)
{
System.out.println("this is a test.");
}
}
But if I add a package:
package test;
public class Tester
{
public static void main(String[] args)
{
System.out.println("this is a test.");
}
}
I got this error
Error: Could not find or load main class Tester
[Finished in 6.8s with exit code 1]
[cmd: ['javac "Tester.java" && java "Tester"']]
[dir: /Users/ph/Documents/JAVA/test]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
Any idea why is this happening or how to fix it?
[cmd: ['javac "Tester.java" && java "Tester"']]
[dir: /Users/ph/Documents/JAVA/test]
Sublime Text is trying to compile your program in a directory called "test" (see #2), which is the package name. It is looking for a file named "Tester.java" within that directory (see #1), but it doesn't exist because "Tester.java" is inside the current directory ("JAVA").
When compiling Java files in packages, the files need to be in a directory structure that reflects the package hierarchy. So you need to move your file to the directory that corresponds to the package it is contained in. For example, class "A" in package example.utils.letters would have to exist at the path ../example/utils/letters/A.java
Create folder "JAVA/test" and move Tester.java into there, then run it.
Related
I have created a simple java project "DesignPatterns".
I created a file FacadePattern.java with the path being ~/DesignPatterns/Structural/FacadePattern/FacadePattern.java
My FacadePattern.java class,
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But my editor (VSCode) gives me an error at the start of the file:
The declared package "" does not match the expected package "Structural.DecoratorPattern"
So upon clicking on quick fix, it added package Structural.FacadePattern; in the first line.
So the final code became
package Structural.FacadePattern;
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But when I compile the above file using the command
javac FacadePattern.java && java FacadePattern
It is giving me the following error:
Error: Could not find or load main class FacadePattern
Caused by: java.lang.NoClassDefFoundError: Structural/FacadePattern/FacadePattern (wrong name: FacadePattern)
After searching a lot in the internet, I ran the following command:
javac -sourcefile ~/DesignPatterns FacadePattern.java && java FacadePattern
But no use, I am getting the same error.
Can anyone explain why my editor gave an error but code ran successfully before? and why wont it compile after adding "package Structural.FacadePattern;" line? and what is -sourcefile parameter in javac command? and how to run the code with successfully without errors in editor as well as the terminal?
Stick to naming rules. Package names should be lowercase only. In your case, it should be package structural.facadepattern;
Run the 'correct' class. Because your class is not inside 'the base folder' aka the 'default package', you have to prefix the class with its package name: java Structural.FacadePattern.FacadePattern or rather java structural.facadepattern.FacadePattern if you use the proper case for packages.
I'm a relative Java newbie so apologies if the question appears somewhat basic. I've googled high and low for an answer here and I'm not finding anything that's helping.
Problem:
Whilst I'm able to integrate external packages into my Java programs from an IDE environment, I am trying to do run a very basic program from the command line that calls on a separate, basic package file that I have written - and am simply doing all this as I want to have a bottom-up understanding of how package files are related to a main program by Java.
I have a program that sits on my desktop named MyProgram.java:
import org.somepackage;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
Still on the Desktop, I then have another folder which I've named src, inside of which I have created the necessary subfolders corresponding to the package name, i.e. ./src/org/somepackage - and in this location, I have the Human.java file which defines the Human class with the following contents:
package org.somepackage;
public class Human {
public void scream() {
System.out.println("I exist!!");
}
}
I then created a classes folder, again on the Desktop, and ran the following compile command on the command line:
javac -d ./classes/ ./src/org/packagename/Human.java
This ran fine and created - as expected - the Human.class file within the ./classes/org/packagename/ location.
However, where I fall down is when I then try to compile MyProgram.java on the command line, i.e.
javac -cp ".:./classes/" MyProgram.java
As you'll see, my class path contains a reference to the current location (".") for the MyProgram.java file, and it contains a reference to the classes folder ("./classes/") which is the base location for the org.somepackage package inside whose subfolders (./classes/org/somepackage/) on can find the Human.class file.
At this stage, I was simply expecting the java engine to compile MyProgram.java into the program MyProgram.class - but, instead, I get an error:
MyProgram.java:1: error: package org does not exist
I've been following the instructions listed here:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html
and I don't appear to be deviating from the instructions - yet I'm unable to locate an explanation on Stackoverflow or anywhere else as to a possible reason for this compile failure. If anyone has an idea, your help would be very much appreciated.
Thanks,
Your mistake is here
import org.somepackage; <--
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
you forgot to import class actually, you need to write this name
import org.somepackage.Human; import all package content import org.somepackage.*; or write full qualified name of class in your code
org.somepackage.Human myHuman = new org.somepackage.Human();
myHuman.scream();
correct mistake:
import org.somepackage.Human;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
after that compile your Human.java by this command:
javac -d classes Human.java
and MyProgram.java
javac -d classes -cp "classes" MyProgram.java
and run MyProgram by
java -cp "classes" MyProgram
For some reason, I can't run ANY programs that begin with a package declaration.
Let's say I am trying to run a simple program called 'HelloDate.java'.
package Test;
import java.util.*;
public class HelloDate {
public static void main(String [] args) {
System.out.println("Hello, it's: ");
System.out.println(new Date());
}
}
Both HelloDate.java and HelloDate.class are located in the same folder:
/Users/eduarddedu/Desktop/Test
I am trying to run HelloDate from inside the 'Test' folder; 'pwd' returns:
/Users/eduarddedu/Desktop/Test
The CLASSPATH variable is not set to anything: echo $CLASSPATH returns an empty line.
To my mind, I should now be able to run the program with the command:
java HelloDate
But all I get is this: Error: Could not find or load main class HelloDate
I have also tried setting the CLASSPATH to (alternatively) :
/Users/eduarddedu/Desktop
/Users/eduarddedu/Desktop/Test
Still nothing works.
If I delete the package declaration at the begining, I can run the program just fine, from inside the 'Test' folder or from anywhere else, by setting the CLASSPATH variable.
You are running the file from the wrong directory.
Go to /Users/eduarddedu/Desktop and run:
javac Test/HelloDate.java
java Test.HelloDate
You should call java Test.HelloDate from outside the Test folder.
I'm just learning java and following a book.
I have a program written via text editor and run commands via cmd.
I've complied 1 program thru javac and executed thru java no problem. (Hello)
Then I modified that to add a comment to the class, named file Hello2.java. I compiled it with no problems, but upon execution, I receive this error: Could not find or load main class Hello2.
I have classpath and path set correct;y on environment variables.
Ideas?
UPDATE
Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Hello2.java
//Filename Hello2.java
//Written by
//Written on
public class Hello2 {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
/*This class demonstrates the use of the println() method to print the message Hello, world! */
I've found the solution to my problem. I know it's not a code problem. But what I did is that I deleted CLASSPATH from system variables and everything now works...at least for now.
Thanks a lot everyone for your inputs, much appreciated!
You have to change the name of the public class too when you change the name of the file. So, if your file is called Hello2.java, the class should be called Hello2 and not Hello.
Are you sure you set the classpath correctly? Why don't you try running java -cp the directory of the .class file Hello ? If that doesn't work please upload the full stacktrace.
You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath.
Note that the windows classpath separator is a semi-colon ie ;
If your class file is saved in following directory with Hello2 program name
d:\sample
java -cp d:\sample Hello2
java -cp . Hello2
I believe you have Hello2.java file as below.
class Hello {
public static void main (String args[]) {
System.out.println("Hello");
}
}
Change it to
class Hello2 {
public static void main (String args[]) {
System.out.println("Hello");
}
}
The change is class Hello2 instead of class Hello.
Note : You should always have classname and file name SAME.
Good Luck!!!
Update 1
Are you doing below steps??
Wrote Hello.java
Compile by javac Hello.java
Run by java Hello
Rename Hello.java to Hello2.java
Rename class name i.e. class Hello to class Hello2
javac Hello2.java
java Hello2
I believe you are missing Step 6 & executing step 7 after step 5. Please confirm.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
package in .java file makes class file unuseable
My Hellow World runs fine.
But as soon as I add a package reference I cannot run it from the command line:
package pv;
public class hcw2 {
public static void main(String[] args) {
System.out.println("Hello Cruel World.");
}
}
compiles fine, then I expect to use java pv.hcw2 to run it, as:
>java pv.hcw2
>Error: Could not find or load main class pv.hcw2
I have also tried just java hcw2, to no avail.
Running in the same directory as the original which runs. Running on Windows 7 64b.
Thank you
You should have a folder called pv under which your file hcw2.java should lie. The folder pv is nothing but your package. Then outside the directory you may issue a javac command as shown below followed by java.
braga#braga-laptop:~$ javac pv/hcw2.java
braga#braga-laptop:~$ ls pv
hcw2.class hcw2.java
braga#braga-laptop:~$ java pv.hcw2
Hello Cruel World.
You have to keep your class in the folder named your package. so for :
package pv;
public class hcw2 {
public static void main(String[] args) {
System.out.println("Hello Cruel World.");
}
}
The hcw2.java should be kept like pv\hcw2.java
once you compile successfully there should be the class file in same folder like :
pv\hcw2.class
While running you have to change directory to the base directory. so if your directory structure like : d:\java\pv\hcw2.java
then
Change dir to d:\java>
Run the java command there with the package name. So :
d:\java> java pv/hcw2 or
D:\java>java test.Test
Your java class needs to be inside a subdirectory called pv. So:
mkdir pv
mv hcw2.java hcw2.class pv
then you can run
java pv.hcw2