Unable to run packaged TestMain.java - java

I am making Java files in my package programapper, and have package programapper; at the top of every file. One of the files is called TestMain.java:
package programapper;
import java.util.*;
import java.util.List.*;
import java.lang.*;
import java.io.*;
class TestMain {
public static void main(String args[]) {
String filename = null;
System.out.print("File to read: ");
Scanner in = new Scanner(System.in);
}
}
Whenever I compile, I run
javac -d . *.java
This compiles fine and I am left with all of my .java files in my primary Programapper/ directory, and then I have Programapper/programapper, where all of my class files are stored.
I am unable to then load the class and run its main function. I'm using
java -classpath .:programapper/ programapper.TestMain
and I have also tried using
java -classpath .:programapper/ TestMain
The error I am receiving is Error: Could not find or load main class programapper.TestMain

You need to make the class public:
public class TestMain {
...
If you omit visibility, the class will be accessible only from the classes within the same package. Which is not sufficient for the main class.
Additionally, instead of specifying just the class name, you need to specify classpath as "." and specify the full class including package:
java -classpath . programapper.TestMain

Related

How to determine if java package is installed? [duplicate]

I have a directory structure like com/example/web under the root directory which contains a java file Bear.java. I have another java file BearExtra.java in directory structure com/example/model in same root directory as above. I am calling a method in BearExtra.java from Bear.java and I am getting the error that the package does not exist.
I have imported com.example.model package in my java file. Can give me some advice?
This works:
com/example/model/BearExtra.java
package com.example.model;
public class BearExtra {
public static void go() {
System.out.println("Yay, it works!");
}
}
com/example/web/Bear.java
package com.example.web;
import com.example.model.*;
public class Bear {
public static void main(String[] args) {
BearExtra.go();
}
}
Now, to compile and run these classes, go to the directory where you can "see" the com folder and do:
*nix/MacOS
javac -cp . com/example/model/*.java com/example/web/*.java
java -cp . com.example.web.Bear
Windows
javac -cp . com\example\model\*.java com\example\web\*.java
java -cp . com.example.web.Bear
and the following is being printed to the console:
Yay, it works!
Did you specify the 'package' in your class files?
Bear.java
package com.example.web;
import com.example.model.*;
class Bear { ...
BearExtra.java
package com.example.model;
public class BearExtra { ...

Must a class name be fully qualified when we already set -classpath to locate that file?

Example:
I have a class called ProgA
package test;
public class ProgA
{
public static void main(String[] args)
{
ProgB pb = new ProgB();
pb.callMe();
}
}
Now I have the ProgB like below:
package test2;
public class ProgB
{
public void callMe()
{
System.out.println("inside callme");
}
}
After compiling ProgB.java its class file is generated in the test2 package. Now when I try to compile ProgA.java using this command:
javac -cp C:\Users\MyName\Desktop\test2 ProgA.java
I get the error that it cannot find ProgB.
My question is why cant java look inside the class path to find ProgB.class file and compile my ProgA.java successfully? The code works fine when I specify the fully qualified class name of ProgB inside ProgA.java code and run with the classpath set to -classpath C:\Users\MyName\Desktop. Why to have the fully quilified name when I am already specifying the full class path to find ProgB. I am not clear with that concept of classpath and fully qualified class name. Please explain me. Thank you
First you would need to import the class. This is why it asks you to use a fully qualified class name. You cannot use a class that is not in the same package without importing it (or using the fully qualified class name).
import test2.ProgB;
Then while compiling, you should provide the class path till the root location, the compiler will look for the class using the package name as the path.
Your compile command should be.
javac -cp C:\Users\MyName\Desktop ProgA.java
In order to use a class from another package, you need to either use the fully qualified class name, or have an import statement. This is a .java source code requirement. It can't be fixed simply by fiddling with the compiler's classpath.
Without an import statement, unqualified names are assumed to belong to the current source file's package. If you're in a package test file, the identifier ProgB will match test.ProgB but not test2.ProgB. The compiler won't search other packages unless you tell it to.

Java file layout without IDE (OS: Ubuntu)

I am writing some Java code without IDE, I got a little problem when I try run the code after compiling it. (I am using Ubuntu 64)
$ javac ClassName.java
$ java ClassName
Could not find or load main class ClassName
My directory structure is the following:
Projectname
----- PackageName
---------- className.java
---------- className.class
My code start by writing down the packageNmae. When I remove the package statement, it works. While error occur when that statement is included.
package PackageName;
public class myClass {
// .... to be used in the main class
}
public class ClassName {
public static void main(String args[]) {
// ....
}
}
Could anyone tell me what is the problem.
The main issue is where are you trying to run the command from. You do not run it from inside the package directory, but from the root of your package tree (in your example, the Projectname directory).
From there, you should be doing:
javac PackageName.className
which tells it to compile "className"[sic] inside package "PackageName"[sic]. The way you are doing it, you are telling it to compile a class that is not part of a package (which is strongly discouraged).
Notes:
Each file can only have a "general" class, with the name of that file.
You may define inner classes inside a class, but that would be inside the code block of the class.
package packageName;
public class ClassName {
public class InnerClass {
...
}
public static void main(String args[]) {
...
}
}
File name and class name must be the same. That includes case (lowercase or uppercase) of the name.
Class names always begin in uppercase.
Package names should be in camelCase.
Usually you do not want to leave your compiled (.class) files with the source (.java) files. The usual structure is, at the minimum:
--> Project --> src --> myPackage --> MyClass.java
--> bin --> myPackage --> MyClass.class
so you only need to copy your .class files to distribute the executable.

How to import my own class?

I have this java class :
package myClass;
public class myClass
{
private int herAge ;
public void setHerAge (int herAge)
{
this.herAge = herAge ;
}
}
and I want to import this class after compile it in another source file called Test.java exists in the same directory , and here what Test.java contains :
import myClass ;
public class Test
{
public static void main (String []args)
{
myClass Rudaina = new myClass();
Rudaina.setHerAge(30);
}
}
when I compile Test.java I see this in my console :
Test.java:1: error '.' expected
import myClass ;
^
Test.java:1: error '.' expected
import myClass ;
^
Your class myClass is also in package called myClass, which can be a cause of confusion.
Try:
import myClass.myClass;
This is called the fully qualified name of the class.
A package is meant to group classes which are conceptually related. Having a package named after a single class is not very useful.
You could name your package after the name of your project. For instance, you could name it
package assignment1;
And then import your class like this:
import assignment1.myClass;
While what everyone wrote is true, since the two files are in the same directory, no import should be necessary.
FYI, it is customary to capitalize the names of classes.
It's better to put the same package name or a different one as mention below on top of the Test.java file;
package myClass; //or some other name also viable
Then when you compile you can do like this;
javac -d . myClass.java
javac -d . Test.java
The -d specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows), etc. If you want to keep the package within the same directory, you can use the .(dot).
After that use the import statement inside Test.java like this;
import myClass.*;
or
import myClass.myclass;
After that when you run the Test class do like this;
java myClass.Test //myClass in here is package name if you use some different package name use that
You should use the whole class name - including the name of the package.
You are missing the package name. Try import myClass.myClass;

How to run Java Program with class having sub class in different package hierarchy?

I have two classes like:
Class GetData is like:
package com
public class GetData{
private String name = "John";
public String getName(){
return name;
}
}
package com.test;
import com.GetData;
public class Test{
public static void main(String args[])
{
GetData data = new GetData();
System.out.println("Welcome "+data.getName());
}
}
When I'm trying compile the Test class it throws an error like undefined symbol for GetData. I tried like:
javac -cp .com/GetData.class Test.java
Kindly help me out
The problem is with how you're specifying the classpath. You just need to specify the directory which is at the root of the directory containing the classes, so if you've got com/GetData.class you just need:
javac -cp . Test.java
Although you should really have the source code in a directory hierarchy matching the packages, so I'd expect
javac -cp . com/test/Test.java
Then run with:
javac -cp . com.test.Test
All of this is assuming that . isn't on your classpath - it is by default, so you may well find you can just use:
javac com/test/Test.java
java com.test.Test
Its the problem how you are running the code. Simply you can run with eclipse Run As->Java Application if you are not sure how to run through command console.

Categories