Java via the Linux command line with packages [duplicate] - java

This question already has answers here:
How do I run a Java class in a package?
(5 answers)
Closed 3 years ago.
I have depended on using IDEs all the time for Java and would like to use the terminal to understand more.
I have a Java application called test.java. It depends on other jar files to compile and run.
The first line of my application also creates a package as follows
package package1;
Now, when I compile this with :
javac -cp .:"JAR FILE PATHS HERE" test.java
It compiles fine. However when I try to execute it as follows:
java -cp .:"JAR FILE PATHS HERE" test
I get the error Error: Could not find or load main class test
If I don't create a package in my application with package package1; , it executes fine.
How do I execute it if I do create package1 tho? using path package1/test doesn't work

You need to double check your folder structure- as you know, when using folders directly (instead of jar files), packages are subfolders under the classpath (see the complete doc, or read below for an example).
That means, in your example you should have the folder structure:
workingFolder
\- package1
\- Test.java
\- Test.class
From workingFolder, you run javac package1/Test.java ; that produces Test.class under package1.
To run, from workingFolder you run java -cp . package1.Test.
The folder workingFolder is in the classpath, so package1.Test is resolved as package1/Test.class; the package1 folder is basically the package1 package.
edit and the Test class must be in the right package, like:
package package1;
class Test {
public static void main(String[] v) {
System.out.println("hw!");
}
}
Also, see https://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html for a good explanation on how to compile and run a Java program using the command line.

Related

why folder structure is not created even though I had taken package statement? [duplicate]

This question already has answers here:
What happens if you compile an empty java file?
(9 answers)
Closed 5 years ago.
I was trying a package concept in java.
I created a Test.java file that only contains a single statement:
package pack1;
When I compile the file using the command as:
javac -d . Test.java
it compiles fine but doesn't create the .class file, nor a folder named pack1.
I don't know why. Can anyone please suggest?
If I change the code to:
package pack1;
class Test{}
and compile the program by using the command
javac -d . Test.java
Then it compiles fine and folder structure is also created correctly.
Please suggest me why it happens that way?
In your original code, you haven't declared any classes - so there wouldn't be any class files. If there are no class files, presumably the compiler sees no need to create the directory structure that would have been necessary for any class files it would have created.
In practice, the only time I've ever seen a Java source file with no classes in is for package information - and that's pretty pointless if there are no classes in the package, too, although it's possible that if you run javadoc it will create an entry for the empty package. As noted in comments, package annotations will end up creating a class file, but they have to be in package-info.java. For example:
// In package-info.java
#Deprecated
package foo;
Compiling that will create package-info.class, creating a directory for it if necessary.

Compiling and running a java program with complex file structure from the commandline

Trying to compile and run my java program from the commandline that is set up a bit weird. The file structure is as follows:
[ROOT]/
|
|____libs/
| |____myExtraJar.jar
|
|____src/
|____main/
|____com/
|____example/
|____myClass.java
The package is defined at the top of the java file as
package com.example;
I am able to compile the program fine (I think) while in the root folder, using
javac -classpath "/libs/myExtraJar.jar" src/main/com/example/*.java
I don't get any compilation errors (such the ones that occur if I leave off the classpath) and I can see that .class files are being created in the com/example/ folder. However, I can't find any way to run the compiled program. Running
java src/main/com/example/myClass
results in the message
Error: Could not find or load main class src.main.com.example.myClass
Any help would be appreciated.
You need to specify the classpath when you run it, and you also need to use the fully-qualified classname. Like,
java -cp "libs/myExtraJar.jar:src/main" com.example.myClass
Elliot is right. More precisely, you need to add the build directory to your classpath. It is the directory containing your *.class files, and is sometimes named target/.
$ java -cp "target:lib/myExtraJar.jar" com.example.myClass
Moreover, src/main/com/example/myClass should be com.example.myClass, which is the fully-qualified class name. See http://www.manpagez.com/man/1/java/ for details of the java command.

running selenium tests from command line junit [duplicate]

This question already has answers here:
How to run a java class with a jar in the classpath?
(4 answers)
Closed 7 years ago.
I am unable to execute selenium tests(JUNIT) from command line
my project folder path class file
C:\Users\CP042756\workspace\BLR_demo1\bin\com\analytics\logindash
File :LoginTest.class
my project folder path java file
C:\Users\CP042756\workspace\BLR_demo1\src\com\analytics\logindash
File:LoginTest.java
jar file folder: C:\jars\imp\selenium-2.45.0\libs
jar fiLe: junit-dep-4.11.jar
it runs properly in Eclipse
i want to run it in command line
i have tried the following commands from the command line
1)
java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar:C:\Users\CP042756\workspace\BLR_demo1\bin\com\analytics\logindash org.junit.runner.JUnitCore LoginTest
Error:Could not find or load main class
2)java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar org.junit.runner.JUnitCore LoginTest
Error:Could not find class:Login test
Exception in thread main java.lang.noclassdefounderror
Please help,
You have to use semicolon as path separator in Windows. Then your first example should work.
For class files there are two different rules. check, which one applies to your situation:
For .class files in an unnamed package, the class path ends with the
directory that contains the .class files. For .class files in a named
package, the class path ends with the directory that contains the
"root" package (the first package in the full package name).
So, for the latter try this:
java -cp C:\jars\imp\selenium-2.45.0\libs\junit-dep-4.11.jar;C:\Users\CP042756\workspace\BLR_demo1\bin org.junit.runner.JUnitCore com.analytics.logindash.LoginTest

Cannot run simple HelloWorld class [duplicate]

This question already has answers here:
Cannot run simple compiled java program?
(4 answers)
Closed 8 years ago.
I'm trying to compile simple Java HelloWorld source on Windows. I compile it the following way:
javac HelloWorld.java
But then when I run it like this
java HelloWorld.class
I get an error
Could not find or load main class HelloWorld.class
But the file is there, any hints?
Run it like this:
java HelloWorld
Do not put .class suffix after the class name.
java -cp . HelloWorld
The . is needed in order to tell Java to include the current directory in the classpath.
HelloWorld is the name of the class to run (must not add the .class suffix).
This is rather a basic step towards Java development and it's important!
say you have a Java file named: Main.java , open it by your favorite editor:
public class Main
{
public static void main(String[]args)
{
System.out.println("Rugal");
}
}
Now just exit your editor and use javac to compile:
javac Main.java
which will generate a Main.class file.
Then you can use java to launch a JVM to execute this main method in class Main.
java Main
notice that as your class name is Main thus you need to execute the Main class.
here you need not to include .class suffix after the class name.
If you have package name in this class, just use:
java your.package.name.Main to execute.
Yes, is that easy? start your journey in Java!
javac HelloWorld.java is ok But then
use
java HelloWorld
`
To run java program
java HelloWorld
(w/o .class extension)
[NOTE]
Tutorials for beginners http://www.javabeginner.com/
Compiling your java file using javac HelloWorld.java is fine but when your try run it do like this. java HelloWorld.
**
NOTE : Use only class name while running your compiled code.

package in .java file makes class file unuseable

It's been too long since I've last done Java, and I can't remember why the following happens:
Given this file, created by a standard Maven project, as can be seen here: Maven Tutorial
package com.mycompany.app;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Compiling this, not even with Maven but with the standard javac executable, will generate a class file without errors. Try to run the class file, and you get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: App (wrong name: com/mycompany/app/App)
Remove the package command, compile again and it runs just fine. Why is this? I'm running JDK 1.6.0_21 btw.
One thing you must do after creating a package for the class is to create nested subdirectories to represent package hierachy of the class. In your case the package name is "com.mycompany.app" so the App.class (compiled App.java file) should reside in "com/mycompany/app" sub-directory. It doesn't matter where the source file is residing though. For example, I have copied your file and did the following:
$ ls
App.java
$ javac App.java
$ ls
App.class App.java
$ mkdir -p com/mycompany/app
$ mv App.class com/mycompany/app/
$ java com.mycompany.app.App
Hello World!
$
Please read Wikipedia page about Java Packages for more information. You can also take a look at these links:
The Java packages tutorial
Java packages tutorial
Oracle's notes on packages
Good luck!
When you attempt to execute your program, it will look for the class file using the path specified in the package. So, when you have the package statement in the file, your class file must be in the com/mycompany/app/ directory (relative to what directory you're attempting to run it from); if it can't find it, you get that exception.
Thus, when you remove that package statement, the JVM will look for it in current directory, which is why it works (because you're executing java App in the same directory in which the App.java and App.class files exist).
You need to add the com/mycompany/app folder to your Java CLASSPATH . If I remember well, you can also do it from the cmdline using the parameter "-cp".
This is because in Java filesystem files map to classes (e.g. each public class must be in a separate eponymous file) and packages map to directories.
So if you have a class which is in the com.mycompany.app package it must be in com/mycompany/app directory relative to the classpath.
In your case you should have an output directory, say and the you should have the class in /com/mycompany/app/App.java. Then you build it, running javac from and giving com/mycompany/app/App.java as parameter, instead of com/mycompany/app/App.java.
Running the class works in an analogical way, but you give the fully-qualified-name of the class, instead of the directory path.

Categories