Java Packages/Library Functions - java

I am trying to create a package of library functions for a main Java program but I am have some issues.
I don't know much about about Java packages and I am going through some documentary online.
I have created my directory as such
./Program/Program.java
./Program/TestFunc.java
./Program/classes/library/
The contents of TestFunc.java are
package library;
public class TestFunc {
public void message01() {
System.out.println("called message01");
}
public void message02() {
System.out.println("called message02");
}
}
I compiled it as I read in the documentation
javac -d ./Program/classes TestFunc.java
Which gives me
./Program/classes/library/TestFunc.class
Then I try to call it in Program.java
import library.*;
public class Program {
public static void main(String[] args) {
System.out.println("Starting Script");
}
}
When I try to compile using
javac -d ./Program/classes Program.java
I get the error
package library does not exist
Why is this?

You've used -d which says where to put the output, but you haven't told it that the same directory should also be used for input on the classpath. Use the -cp option for that:
javac -d classes -cp classes Program.java
(It's not clear whether you're trying to do this from inside the Program directory, or above it - your source filename appears to be inside the Program directory, but you're specifying the output directory as if you were in the directory above...)

Related

class file contains wrong class error while accessing jar file

I am trying to create a JAR file for my two source codes Serial.java and DBaccess.java.
I am doing this as a learning exercise ,the classes do not do anything,they are dummy classes.
OS - Windows 10
JDK - GraalVM
Listing of my source code.(Serial.java)
public class Serial
{
void open()
{
System.out.println("Serial Port Opened - 0");
}
}
Listing of my source code.(DBaccess.java)
public class DBaccess
{
String DBname = "MySerialDB";
void write()
{
System.out.println("Data Written to DB - W");
}
void read()
{
System.out.println("Data Read from DB - R");
}
}
I have compiled them to .class files using javac
javac DBaccess.java
javac Serial.java
I created a folder
H:\myclass\com\mydomain\util
and manually copied the class files to util directory
and created a jar file myjar.jar of this format
H:\myclass>H:\graalvm\bin\jar tf myjar.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/mydomain/
com/mydomain/util/
com/mydomain/util/DBaccess.class
com/mydomain/util/Serial.class
I want my source code JMainEntry.java to access the classes in the above jar file myjar.jar.
JMainEntry.java
//Main Class of the Program
import com.mydomain.util.*;
public class JMainEntry
{
public static void main (String[] Args)
{
Serial myS = new Serial();
myS.open();
DBaccess myDB = new DBaccess();
myDB.write();
myDB.read();
}
}
When i compile the code
H:\jar-compile>H:\graalvm\bin\javac -cp myjar.jar JMainEntry.java
I am getting the error
bad class file: myjar.jar(/com/mydomain/util/Serial.class)
class file contains wrong class: Serial
Please remove or make sure it appears in the correct subdirectory of the classpath.
I am not able to understand why javac is complaining like this,Can anyone help?
bad class file: myjar.jar(/com/mydomain/util/Serial.class)
Make sure you declare
package com.mydomain.util
at the top of all .java files in the directory com/mydomain/util, including Serial.java. To understand this better, read about packages.

How do I compile my main Java program on the command line when it includes an external java package?

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

how to compile and run two different java programs with instances of one in another which are in different directories?

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).

Enthuware Mock-20 Confusion -classpath and packages

I am using enthuware to practice mock questions for classpath and packages. Here is the question.
//in file ./Foo.java
public class Foo {
public static void main(String[] args) {
System.out.println("In Foo");
}
}
//in file ./com/Foo.java
package com;
public class Foo {
public static void main(String[] args) {
System.out.println("In com.Foo");
}
}
Which of the given statements are correct assuming that the command lines are executed from the current directory with default classpath set to ./classes?
The options given are
Executing java -classpath .:./com Foo will print "In com.Foo"
Executing java -classpath ./com:. Foo will print "In com.Foo"
Executing java Foo will print "In com.Foo"
java -classpath . com.Foo will not execute.
Executing java -classpath ./com:. com.Foo will print "In com.Foo"
Correct option given is option-5. The strange problem is when i try to execute option-5 from my command line it gives me the following error.
Can someone tell me what is wrong? I am not able to figure out the reason. Plus what does this
./com classpath mean?
Command Change
I noticed one strange thing, if i change the classpath and run the command as
java -classpath . com.Foo
It states in com.Foo. As soon as i change the command to add this path ./com. It gives the above mentioned error.
Thanks.
The "./com" is a relative path to the current path.
If you have your class in [current_path]/com/Foo.class
You can run your class with the following command:
java -classpath ./com com.Foo
If you are in the com folder [current_path: com]/Foo.class you can execute the following command:
java -classpath . com.Foo

Can i somehow run previously compiled java bytecode from a new Java program?

Is it possible to first compile a set of Java source code files into bytecode, and later run that bytecode-- not directly, but by adding commands to another java program-- such that this new java program (in its various classes/functions) runs the previously compiled java bytecode?
If this is possible, then what is/are the required command(s) to do this?
Absolutely - and that's what libraries are all about! They're typically distributed as jar files, but you don't have to use jar files in order to reuse the code.
All you need to do is make sure that it's on the classpath at both compile-time and execution time.
For example, create the following files:
lib\p1\Hello.java:
package p1;
public class Hello {
public static void sayHi(String name) {
System.out.println("Hi " + name + "!");
}
}
app\p2\Greeter.java:
package p2;
import p1.Hello;
public class Greeter {
public static void main(String[] args) {
Hello.sayHi(args[0]);
}
}
Now let's compile our "library":
$ cd lib
$ javac -d . p1/Hello.java
$ cd ..
And now, by adding that to the classpath, we can use it in our "app":
$ javac -d . -cp ../lib p2/Greeter.java
$ java -cp .:../lib p2.Greeter Jon
Hi Jon!
(This all works on Windows with the one change of using ";" instead of ":" in the joint classpath on the last line.)

Categories