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.
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 have recently set up the Java extension for VS Code.I have a folder that contains two files Main.java and Person.java. Main.java calls Person.java. When I set a breakpoint in Main.java, everything works as normal.However, when I set a breakpoint in Person.java, it just skips over it.
Are there any workarounds in this issue?
Main.java file
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
Person person = new Person();
person.speak();
}
}
Person.java file
public class Person {
public void speak(){
System.out.println("Speak!");
//breakpoint on this line right here gets skipped
System.out.println("Speak!");
}
}
Run -> Start Debugging, it stops at the breakpoint:
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 a novice programmer in (Java/C++/C#) and I also know Python. I am trying to create a GameEngine in Java that can call Jython scripts, that have access to methods in the Java engine.
I am clueless as to how to approach this. I have already done weeks of research and nothing has answered my question ; that is:
How can I call methods in my Parent class, from my JythonScript, which is executed by my Parent class?
-----------------------------------UPDATE---------------------------------------------------
Okay, The answer here helped me understand some things, but it didn't solve my problem.
What I was wondering if something such as this would work:
class MyJavaClass
{
Public MyJavaClass()
{
PythonInterpreter interp = new PythonInterpreter;
interp.execfile("MyJythonScript.py");
interp.exec("InGameCommand");
}
public void SpawnPlayer()
{}
public void KillPlayer()
{}
}
MyJythonScript.py
Def InGameCommand():
SpawnPlayer()
KillPlayer()
Is this even possible? There a way to do this?
-----------------------------------UPDATE---------------------------------------------------
Location to Jython: "C:\jython2.7a2\jython.jar"
Location to my work: "C:\Documents and Settings\PC\Desktop\Jython*.java"
Location to my local JtyhonJar: "C:\Documents and Settings\PC\Desktop\Jython\jython.jar"
my compiler I wrote:
"#echo off"
"javac -classpath C:\jython2.7a2\jython.jar *.java"
"echo done"
"pause >nul"
now it doesn't even compile... (I've changed little things in my code to see if it changed and it hasn't!)
Yes, this way is fine, but you can not run python script in constructor method, if so, it will be dead recursive at your code. please see the following code. you run PythonScriptTest class, it will run python script first, then python script will invoke PythonScriptTest.SpawnPlayer() method.
java code:
package com.xxx.jython;
import org.python.core.PyFunction;
import org.python.util.PythonInterpreter;
public class PythonScriptTest {
public static void main(String[] args) {
PythonScriptTest f = new PythonScriptTest();
f.executePythonScript();
}
public PythonScriptTest(){
}
public void executePythonScript() {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/home/XXX/XXX/util.py");
PyFunction pyFuntion = (PyFunction) interpreter.get("InGameCommand", PyFunction.class);
pyFuntion.__call__();
}
public void SpawnPlayer() {
System.out.println("Run SpawnPlayer method ##################");
}
}
Python scripts, named util.py:
import sys.path as path
# the following path is eclipse output class dir
# does not contain java class package path.
path.append("/home/XXX/XXX/Test/bin")
from com.xxx.jython import PythonScriptTest
def add(a, b):
return a + b
def InGameCommand():
myJava = PythonScriptTest()
myJava.SpawnPlayer()
need to jython.jar
execute python code in java.
import org.python.util.PythonInterpreter;
public class PythonScript{
public static void main(String args[]){
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('One','Two','Three','Four'); ");
interpreter.exec("print days[1];");
}
}
invoke python script method in java.
python script file, named test.py
def add(a, b):
return a + b
java code:
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class PythonScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/home/XXX/XXX/test.py");
PyFunction pyFuntion = (PyFunction)interpreter.get("add",PyFunction.class);
int a = 10, b = 20 ;
PyObject pyobj = pyFuntion.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("result = " + pyobj.toString());
}
}
run python script in java
python script file, named test.py:
number=[1,10,4,30,7,8,40]
print number
number.sort()
print number
java code:
import org.python.util.PythonInterpreter;
public class FirstJavaScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("/home/XXX/XXX/test.py");
}
}