call another class from java program [duplicate] - java

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);
}
}

Related

How to run Java method with arguments from command line?

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

send argument to main class by command line with external jar command

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]);
}

ProcessBuilder call another java file same package

I am learning how to use ProcessBuilder, I created a package called socketspractice, inside I have 2 classes, I am trying to create a new process where 'Program.java' calls 'test1.java' so it prints 'test1'.
When I use command prompt: "java socketspractice.test1" 'test1' prints, but using Netbeans it doesn't.
The question is, how can I set the path so it works the same way or what else am I missing? I am using Netbeans for this.
Program.java
package socketspractice;
import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder;
public class Program {
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder builderExecute = new ProcessBuilder("java", "socketspractice.test1");
builderExecute.start();
}
}
AND
test1.java
package socketspractice;
public class test1 {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("test1");
}
}
The main issue with ur approach is that when you are starting ProcessBuilder it doesnt know where ur project lies on your machine, because its running as a seperate JVM process.
So please create you project as a maven project and then try to put the compiled jar in classpath and then start the process builder.
ProcessBuilder pb = new ProcessBuilder("java","-classpath",
"<complete location of your jar containing test1>", "socketspractice.test1")

setting java class path through php shell_exec in MAC

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.

How to call java from python using PY4J

I want to call java from python with Py4J library,
from py4j.java_gateway import JavaGateway
gateway = JavaGateway() # connect to the JVM
gateway.jvm.java.lang.System.out.println('Hello World!')
I've got the following error: "Py4JNetworkError: An error occurred while trying to connect to the Java server". It's seems that no JVM is running, how to fix that?
Minimal working example:
//AdditionApplication.java
import py4j.GatewayServer;
public class AdditionApplication {
public static void main(String[] args) {
AdditionApplication app = new AdditionApplication();
// app is now the gateway.entry_point
GatewayServer server = new GatewayServer(app);
server.start();
}
}
Compile (make sure that the -cp path to the py4j is valid, otherwise adjust it such that it points to the right place):
javac -cp /usr/local/share/py4j/py4j0.9.jar AdditionApplication.java
Run it:
java -cp .:/usr/local/share/py4j/py4j0.9.jar AdditionApplication
Now, if you run your python script, in the terminal where the java AdditionApplication is running you should see something like:
>>> Hello World!
package test.test;
import py4j.GatewayServer;
public class AdditionApplication {
public int addition(int first, int second) {
return first + second;
}
public static void main(String[] args) {
AdditionApplication app = new AdditionApplication();
// app is now the gateway.entry_point
GatewayServer server = new GatewayServer(app);
server.start();
}
}
create a new class and run it(import py4j0.8.jar at 'py4j-0.8\py4j-0.8\py4j-java' first),then run python program
You should first start the java program, and then invoke java method from python.
py4j doesn't start jvm, it just connects to the already started java process.

Categories