I am trying to run the following method in Loader.java from the command line:
public static void method(String path, String word)
As seen above, I must pass in the variables path and word, and I want the command line to display the System.out.println()'s in the method.
What command can I run to do this?
Note: when I run the following commands,
javac *.java
jar -cvf Loader.jar Loader.class
java -cp ./Loader.jar Loader
I get the following error:
Caused by: java.lang.NoClassDefFoundError: path/to/Loader (wrong name: Loader)
What must I do to successfully run method from the command line?
Here is minimum reproducible version of Loader.java:
public class Loader {
public static void main(String[] args) {
method("my/path", "my_word");
}
public static void method(String path, String word) {
System.out.println("Output after doing something");
}
}
Just do the following:
javac Loader.java
java Loader
In fact, if you are you Java-11 or above, you don't even need to use the first command i.e. you can directly use the following command:
java Loader.java
However, if you want to create a jar file and execute the class from it, execute the steps given below:
mkdir demo
cd demo
Now create/place Loader.java in this folder. Then,
javac *.java
jar -cvf loader.jar .
java -cp loader.jar Loader
Note that I've used a new directory, demo to make it clear but it is not necessary. Another thing you should notice is the . at the end of jar command which specifies the current directory.
How to process command-line arguments?
String[] args parameter in main stores all the parameters from the command-line e.g. if you run the following program as java Loader my/path my_word from the command-line,
public class Loader {
public static void main(String[] args) {
if (args.length >= 2) {
method(args[0], args[1]);
} else {
System.out.println("Command line parameters are missing");
}
}
public static void method(String path, String word) {
System.out.println("Path: " + path);
System.out.println("Word: " + word);
}
}
the output will be
Path: my/path
Word: my_word
Related
I'm on windows 10 + jdk1.8
Use used maven to create a project named UseNative, package name is mygroup, files are under:
src\main\java\mygroup\
I have UseNative.java:
package mygroup;
public class UseNative {
public native void greet(String name);
static {
System.loadLibrary("UseNative");
}
#SuppressWarnings("static-access")
public static void main(String[] args) {
System.out.println(System.getProperty("java.library.path"));
new UseNative().greet("me");
}
}
Then under powershell:
javac UseNative.java
OK, and then:
javah -classpath . UseNative
or
javah -classpath . mygroup.UseNative
Both says:
Error: cannot find class file for 'mygroup.UseNative'
I tried to remove the package mygroup line from java file, and then it works! But anyhow, I need this line to comply coding standard.
Where does it get wrong, add additional parameter or environment?
Thanks!
I'm executing the programs from command line and using packages in them.
my program file names are TestA.java and TestB.java.
I've executed below initially
javac TestA.java
No issues for the above and it generated the class file as well
for the following i'm observing the issue
javac TestB.java
output :
TestB.java:2: error: '.' expected
import TestA;
^
TestB.java:2: error: ';' expected
import TestA;
^
2 errors
and the TestA.java file is
package a.b;
class TestA {
public static void methodPublic(){
methodPrivate();
}
protected static void methodProtected(){
methodPrivate();
}
static void methodDefault(){
methodPrivate();
}
private static void methodPrivate(){}
}
TestB.java content is :
package a.b;
import TestA;
public class TestB {
public static void main(String args[]) {
TestA.methodPublic();
TestA.methodProtected();
TestA.methodDefault();
}
public static void methodPublic() {
}
protected static void methodProtected() {
}
static void methodDefault() {
}
private static void methodPrivate() {
}
}
I'm executing the javac by navigating to b folder where these two files exist.
I'm executing the javac by navigating to b folder where these two files exist.
You don't want to do that; the fully qualified class name of every class includes the package. They form a tree. Much like your filesystem. From the b folder move up two directories (to the folder containing a - e.g. cd ../.. or cd ..\.. on Windows). Then
javac -cp . a/b/TestA.java a/b/TestB.java
Also, you would normally want that to be written to a "binary" output folder. So
javac -cp . -d bin a/b/TestA.java a/b/TestB.java
Finally, you don't need to import TestA because it is in the same package as TestB. But, if you want to you need
import a.b.TestA;
I run java application in command line linux with external jar like this :
java -cp ".:commons-net-3.6.jar" FtpClass
how can I send argument to main class by command line ?
You need to specify arguments after class like this
java -cp ".:commons-net-3.6.jar" FtpClass A B C
Assume example
public class Example {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
The following example shows how a user might run Example.
java Example Drink Hot Java
output is
Drink
Hot
Java
if you add this command :
java -cp ".:commons-net-3.6.jar" FtpClass "test1" "test2"
after you can use this main method:
public static void main(String[] args) {
FtpsTest test = new FtpsTest();
test.putFile(args[0],args[1]);
}
I am trying to run a java file from php through shell_exec . It works for simple jar files i.e. jar files with single class. Out of the below 2 commands the first one works fine. The second one consists of a package with two class , so to call particular class from it , I followed this calling procedure. Both the commands works fine in terminal. But the second command fails in shell_exec.
<?php
echo shell_exec("java -jar First.jar hi php");
echo shell_exec("java -cp samlePackage.jar:. samplePackage.Test");
?>
Here is First.class
class First
{
public static void main(String args[])
{
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println("hello");
}
}
And here are the samplePackage classes
package samplePackage;
public class Hello
{
public void sayHello()
{
System.out.println("Hello");
}
}
package samplePackage;
public class Test
{
public static void main(String args[])
{
Hello obj = new Hello();
obj.sayHello();
}
}
I am not able to figure out my mistake. Please help.
Thanks in advance.
This question already has answers here:
How to open the command prompt and insert commands using Java?
(9 answers)
Closed 9 years ago.
I have 2 classes one is a simple one
Sample.java
public class Sample {
public static void main(String args[]) {
System.out.println("Hello World!!!!!");
}
}
Other one is something like this
Main.java
public class Main
{
public static void main(String[] args) throws Exception
{
Runtime.getRuntime().exec("java Sample");
}
}
I am basically trying to run the Main.java program to call Sample.java in a new command prompt...that is a new cmd that should open and print the output of Sample.java...how should I do this...???
Compile the two together, and then from Sample,
Main.main(args);
will do the trick. You don't need to import since you're in the same package.
Note the linked tutorial.
http://docs.oracle.com/javase/tutorial/java/package/index.html
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"cd <where_the_Sample_is> && javac Sample.java && java Sample\"");
or if the class is already compiled:
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"cd <where_the_Sample_is> && java Sample\"");
I am using eclipse. The class files are placed in the bin directory located under the projects directory. The below code starts command prompt, changes directory to bin and issues java Sample command. You can edit it up to your requirement.
Runtime.getRuntime().exec("cmd.exe /c cd \"bin\" & start cmd.exe /k \"java Sample\"");
You can use this code:
public class Main {
public static void main(String[] args) throws Exception {
Class<Sample> clazz = Sample.class;
Method mainMethod = clazz.getMethod("main", String[].class);
String[] params = null;
mainMethod.invoke(null, (Object) params);
}
}