file name first.java
package p1;
public class first
{
int a;
public void display()
{
System.out.println(a);
}
}
second file sec.java
package p2;
import p1.first;
class sec
{
public static void main(String args[])
{
first obj=new first();
obj.display();
}
}
getting this error when i try to run after compilation
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\shaik\Documents\Java> javac -d C:\Users\shaik\Documents\Java first.java
PS C:\Users\shaik\Documents\Java> javac -d C:\Users\shaik\Documents\Java sec.java
PS C:\Users\shaik\Documents\Java> java -classpath C:\Users\shaik\Documents\Java sec
Error: Could not find or load main class sec
file structure must match package structure. You need some directory we'll call 'sourcebase', which contains a dir named p1, and in that dir there needs to be a file named first.java.
Then to run this code after you compiled it, you need a dir we'll call 'classbase', containing a dir named p1, containing a file named first.class. Same goes for your sec class. Once you have all that, you can run:
java -classpath classbase p2.sec
note also that the name of your class is p2.sec. Not sec. No amount of classpath finagling would even make java sec run your app. Within source code, if you're in the same package (so, p2), or you have an import p2.sec; statemente at the top, you get to just type sec to refer to p2.sec but everywhere else, including as argument to the java application, it is called p2.sec.
Related
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
Here's my Situation:
I have a folder Structure:
C:\Users\user\Desktop\JavaTraining\Chapter3\examples.
examples is also a folder. Now, I have a file name Calculator.Java in Chapter3 folder with the package statement package Chapter3;. So, from the Command Line I compiled the file from the JavaTraining directory as javac Chapter3\Calculator.java, It compiled and I see a file Calculator.class generated. But when I run the command java Chapter3.Calculator from the JavaTraining Directory. It throwed me an error: Could not find file or load main class Chapter3.Calculator.
Then, I created a sub folder in Chapter3 named examples and copied the Calculator.java into the examples folder and tried compiling and executing the file thinking Chapter3 as the root folder ( executed commands from the Chapter3 directory). No error, the file got executed.
Please can anyone explain me why this happened or what is the reason behind it, I am going mad...
Calculator.java is just a class Calculator with main function trying to find a sum from a printsum function of two variables.
I went through the suggestions provided in http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean
According to the above, it was either the syntax mistake ( the way of trying to executing the file) or setting the PATH and CLASSPATH environment variables.
I even tried the echo %CLASSPATH% to see if my CLASSPATH variable is set to current directory. It did show me the . when I executed the echo command from JavaTraining directory.
The file did not execute when I tried Chapter3 folder as root directory, but when I create a subfolder in Chapter3 and made Chapter3 as the root directory, it executed, what might be the reason or what am I doing wrong,
Here is the command line with the output:
C:\Users\vikas\Desktop\JavaTraining>javac Chapter3\Calculator.java
C:\Users\vikas\Desktop\JavaTraining>java Chapter3.Calculator
Error: Could not find or load main class Chapter3.Calculator
C:\Users\vikas\Desktop\JavaTraining>cd Chapter3
C:\Users\vikas\Desktop\JavaTraining\Chapter3>javac examples\Calculator.java
C:\Users\vikas\Desktop\JavaTraining\Chapter3>java examples.Calculator
The sum is 30
C:\Users\vikas\Desktop\JavaTraining\Chapter3>
The Calculator.java file:
// One Package Statement
package chapter3;
// The file in Chapter 3 folder, file in example folder has
//package examples;
// One or more import statements
import java.io.*;
import java.util.*;
// Class Declaration
public class Calculator {
// State. Variables and Constants
int i=10;
long k = 20;
// Behavior, one or more methods
void printSum(){
long sum;
sum = i+ k;
System.out.println("The sum is " + (i+k));
}
public static void main (String[] args) {
Calculator c = new Calculator();
c.printSum();
}
}
When you build a file, it is good to have a build directory, then java will put the class in the correct package layout.
mkdir build
javac -d build path/to/source/Files.java
java -cp build package.name.Files
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.
my first java program is
import java.io.*;
class pgm10a
{
public static void main(String args[])
{
pgm10b b=new pgm10b();
b.display();
}
void display()
{
System.out.println("A class");
}
}
it is saved in C:\NNK\pack1
the second program is
import java.io.*;
class pgm10b
{
void pgm10b()
{
pgm10a a=new pgm10a();
a.display();
}
void display()
{
System.out.println("Class B");
}
}
it is in C:\NNK\pack2
I want to run pgm10a but it keeps showing pgm10b not found exception. i have set the class path and compiled for both and both are compiled successfully. but when i try to run them it shows pgm10b not found.
Have a look at the syntax for the java command:
java [options] classname [args]
Anything after the class name is not an option to the java command—it is simply passed as is, in a String array, to the program’s main method.
You can solve your problem by changing your final command from this:
java pgm10a -cp C:\NNK\pack2
to this:
java -cp .;C:\NNK\pack2 pgm10a
The classpath is a sequence of directories, separated by ; when running in Windows (: on other operating systems), which tell the java command where to find compiled classes. If you only specify C:\NNK\pack2, Java will only be able to see classes in that directory. The period (.) refers to the current directory, so the above classpath is pointing to both the current directory (which contains pgm10a) and the pack2 directory (which contains pgm10b).
I have a doubt about -cp and when should I use it. This is my scenario:
I have two .java, the first one:
package autos.tests.paquete;
public class MainAutos {
public static void main(String args[]) {
int x = Integer.parseInt(args[0]);
Respotar objeto1 = new Respotar (x);
int mostrar = objeto1.repostar();
System.out.println(mostrar);
}
}
And the second one:
package autos.tests.paquete;
public class Respotar {
int gasolina;
public Respotar (int gasolina) {
this.gasolina=gasolina;
}
public int repostar (){
int gasolina = this.gasolina +20;
return gasolina;
}
}
Well, I am at root directory, and there, I have that directory: autos/tests/paquete
with both .java.
So I compile:
javac autos/tests/paquete/*.java
And execute from root directory:
java autos.tests.paquete.MainAutos 10
And it works, now here go my doubts:
1) I execute with java -cp . autos.tests.paquete.Main autos 10 and the behaviour is the same.
2) I move the Respotar.class from auto/tests/paquete to another directory, I compile with
java autos.tests.paquete.MainAutos 10 and it works.
3) I move the MainAutos.class from auto/tests/paquete to another directory, I compile with
java autos.tests.paquete.MainAutos 10 and it says: Error: Could not find or load main class autos.tests.paquete.MainAutos
4) I compile with java -cp . autos.tests.paquete.MainAutos (I have the .class on the current directory I am compiling, so I think I have to use -cp .) and it says the same:
Error: Could not find or load main class autos.tests.paquete.MainAutos
Thank you in advance, I hope someone can enlighten me, regards
java -cp is used to set any libraries such as jar file to your current classpath.
For the examples you have shown, you can do it in the below simple way
From you root directory compile your java files using the below command
javac -d . *.java
This would created those appropriate packages and place the class files under them
Then run you code using the command like this from the same root location
java autos.tests.paquete.MainAutos