I dug the internet a lot and found the solution.
One can do this using the following commands:
String fetching = "python " + "c:\\Fetch.py \"" + songDetails + "\"";
String[] commandToExecute = new String[]{"cmd.exe", "/c", fetching};
Runtime.getRuntime().exec(commandToExecute);
Answer from metamemelord on Stack OverflowI've been working on something exciting - PySpring, a Python web framework that brings Spring Boot's elegance to Python. If you're tired of writing boilerplate code and want a more structured approach to web development, this might interest you!
- What's cool about it:
-
Auto dependency injection (no more manual wiring!)
-
Auto configuration management
-
Built on FastAPI for high performance
-
Component-based architecture
-
Familiar Spring Boot-like patterns
-
Recent PRs:
-
Route Mapping Decorators Implementation #3
-
Add Support for Qualifiers and Component Registration Validation
-
-
-
GitHub: https://github.com/PythonSpring/pyspring-core
-
Example Project: https://github.com/NFUChen/PySpring-Example-Project
Note: This project is in active development. I'm working on new features and improvements regularly. Your feedback and contributions would be incredibly valuable at this stage!If you like the idea of bringing Spring Boot's elegant patterns to Python or believe in making web development more structured and maintainable, I'd really appreciate if you could:
-
Star the repository
-
Share this with your network
-
Give it a try in your next project
Every star and share helps this project grow and reach more developers who might benefit from it. Thanks for your support! 🙏I'm actively maintaining this and would love your feedback! Feel free to star, open issues, or contribute. Let me know what you think!
java - Running a Python script from Spring-Boot Web Application on embedded Tomcat - Stack Overflow
Execute Python script from Spring boot
java - How to call a python program from spring boot? - Stack Overflow
Spring Boot and Python machine learning
Videos
A recommended way would be to convert your Python code in a REST microservice and then use that in your Java program.
//u can use ProcessBuilder to load python function which have model call
@Service
public class PythonService {
/**
* Executes a Python script to predict a value based on the given input.
*
* @param input The input value for the prediction.
* @return The predicted result as a double.
*/
public double predict(double input) {
double result = 0;
try {
ProcessBuilder pb = new ProcessBuilder("python3", "train-model.py", String.valueOf(input));
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
if ((line = reader.readLine()) != null) {
result = Double.parseDouble(line);
} else {
throw new RuntimeException("No output from Python script.");
}
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
I'm working on a project that involves utilizing React for the frontend, Spring Boot for the backend, Spring Security for authentication, and Python Flask for machine learning functionalities. I'm considering creating Flask APIs for the machine learning tasks, but I also want to leverage Spring Security for authorization purposes. However, I'm unsure if it's a good practice to have Spring Boot communicate with Flask APIs. Your insights or suggestions would be appreciated!
» pip install pyctuator