Enthuware Mock-20 Confusion -classpath and packages - java

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

Related

Run single junit test with cli

I need to run a single test case run through the cli. I created a runner class
public class Runner {
public static void main(String ... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);
Result result = new JUnitCore().run(request);
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
using this answer
Run single test from a JUnit class using command-line.
I downloaded from github a project (https://github.com/apache/incubator-dubbo) on which I want to run the single method, I positioned myself from terminal on the directory containing the Runner class I created and I launched the following command:
java -cp /path/incubator-dubbo/dubbo-cluster /usr/share/java/junit4-4.12.jar /pathClassRunner/src/com/company/Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat
but I got the following error:
Error: Could not find or load main class pathClassRunner.src.com.company.Runner
can someone help me?
thanks
The command expects you to include any paths in the classpath with NO spaces. A space means that the arguments for the classpath has ended. It there is a space in one of your paths you may include double quotes:
java -cp path1:path2:path3 classname argument
java -cp "path1:path2:path3" classname argument
If a path starts with / it's a full path. If not it's relative to where you run your command. The classname must be the name of the class, not the file, so that means no .class. If your class specifies any package, then the classname is required to contain the package name too, and the path is to the root of the package, not the class itself.
Check that your user has proper access to the files, then try:
java -cp /path/incubator-dubbo/dubbo-cluster:/usr/share/java/junit4-4.12.jar:/pathClassRunner/src/com/company Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat
I'm assuming your Runner is in the default package. In case it's in the com.company package, you'll want to run this instead:
java -cp /path/incubator-dubbo/dubbo-cluster:/usr/share/java/junit4-4.12.jar:/pathClassRunner/src com.company.Runner org.apache.dubbo.rpc.cluster.StickyTest#testHeartbeat
Another assumption is that your Runner.class is in your /pathClassRunner/src... directory.

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

Java Packages/Library Functions

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

Unable to run classes with a package declaration

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.

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