Latex file: 0.tex content:
\documentclass{article}
\begin{document}
\begin{equation}
\frac{\partial p_{dyn}}{\partial n} = A
\end{equation}
\end{document}
Shell file: 0.sh content:
latex /u/b/basnet/Desktop/0.tex
dvipng /u/b/basnet/Desktop/0.dvi
Command in Terminal:
>chmod 755 /u/b/basnet/Desktop/0.sh
>/u/b/basnet/Desktop/0.sh
Output:
Image, Dvi and other files generated by latex. You can see in the blue window.
Problem:
This works only via terminal not by executing the command via Java program.
Java Program Code:
Runtime.getRuntime().exec("chmod 755 /u/b/basnet/Desktop/0.sh");
Runtime.getRuntime().exec("/u/b/basnet/Desktop/0.sh");
System.out.println("Script executed successfully");
Output:
run:
Script executed successfully
BUILD SUCCESSFUL (total time: 0 seconds)
The first line of the code i.e. making the file executable works fine as I cross-checked. But second line where the script file contains the latex command doesn't work when running via Java. I need to generate those files through my program.
I guess this has to do something with environment, I'm not too deep into all this OS stuff, but I think I have a solution which worked for your example on my pc:
You have to change the names of the programs to the full paths for the use in your shell-file, because the java runtime doesn't find them. For example:
/Library/TeX/texbin/latex /u/basnet/Desktop/0.tex
The only thing that goes wrong is, that the output files appear one folder above where the tex file is located, which means you have to change either the output destination or the line for the dvi execution.
If you don't find the path for the programs, open a bash and type
which latex
and
which diving
Related
I am running a java program using a .bat file. It works well after double clicking direcctly on the .bat, that's not the problem.
What I want now, is to run that .bt file (and the java program by extends) from the cmd at first, and then to be able to compile it from any other machine.
I have followed at first the following answer : How do I run a java program from a different directory? , but it didn't work for me.
Here is my .bat file :
#echo on
set CLASSPATH=%CLASSPATH%;.;lib/console.jar;lib/log4j-1.2.13.jar;lib/prog1.jar;lib/prog1_newOption.jar;lib/org.hamcrest.core_1.3.0.v201303031735.jar;lib/RXTXcomm.jar;lib/trace.jar;lib/xercesImpl.jar;lib/xml-apis.jar
java -cp "$/prog1_newOption/src/main/Main" %UsersCommand%
pause
exit
%cmd%
Maybe have I to look for the main path in the .bat file and then parse it the the "java command programm" line?
Thank you so much for your help
I am using a .jar file, but unfortunatley as a black box, i.e. I do not know what exactly is in there nor how it all works.
I am sending commands to the Mac terminal from a Python script. I enter the following command:
java -jar jarfile.jar req_data /abs_path/to/required/data input path/to_my_/input/file.txt
This does what I need: analyses input using the 'black box' and creates and new file with analysis output. This new file is created in the folder where jarfile.jar is located.
I want to have this file put somewhere else upon creation.
I have tried using the > operator, specifying a path, e.g.:
java -jar jarfile.jar req_data /abs_path/to/required/data input path/to_my_/input/file.txt > /output/path/
this created a file in my desired location, but it was simply the message from Terminal, saying "The operation was carried out successfully" - the analysis results file was created in the same folder as before.
I tried %*> too, but it threw an error.
As a poor workaround I now have a function, which retrospectively finds and moves all the newly created files (analysis output) to my desired folder.
Is there a way to control the output files with the command line within the original command? Or is it something that is specified somewhere in my jar file? My problem is that editing it is not allowed.
I'm new to python. However, I may suggest to try few things, if they can work for you. Apology me, if does not work! I believe that you have already done the following step:
import subprocess
subprocess.call(['java', '-jar', 'Blender.jar'])
Like, if you have a properly configured jar path, then can run jar directly.
Secondly, look at the cwd parameter (is used for executable). Include a cwd param as x
def run_command(command, **x):
with subprocess.Popen(command,...., **x) as p:
for run_command specify the path of either the working directory (possibly it should be) or the full system path. I'm not sure, just try both.
for outputline in run_command(r'java -jar jarfilepath', cwd=r'workingdirpath', universal_newlines=True):
print(outputline, end='')
Alternatively, you can try to run command from the directory in which you wish to store output file. Try: run the popen as
subprocess.Popen(r'directory of running command', cwd=r'workingdir')
where workingdir could be your current directory path.
If it does not work, try without r'. If still does not work, try doubling slash in the path like (C:\\ abc\\def)
I've been googling for the better part of 4 hours and nothing I've tried has had any effect. This is your typical JAVA_HOME while configuring android studio on a machine for the first time problem it would appear. I've added literally everything to environment variables and path that I can think of. I've tried reinstalling JRE/JDK/Android Studio many many times. I've tried deleting .android and ..\Local\Temp items. I've tried running android.bat as admin. Nothing helps.
I can run find_java64.exe directly and it actually gives me the correct path, but in the batch file it says it can't find that file. The only thing I noticed is that it says "...Local\Android\sdk\tools\lib\\find_java64.exe" -s with two slashes instead of one after lib. That seems strange but not like it should cause it to not find it... and if I copy-paste the entire path with executable from the error line into command prompt (including the two slashes) it DOES show the JDK location correctly.
Why does the script not work if the line works in isolation? I can't do anything if this batch file isn't going to work.
Edit: I've been trying some more today. The core issue seems to be that when batch files are attempting to run other scripts/executables, windows always throws a is not recognized as an internal or external command, operable program or batch file." error. I've been tracing this through script after script from android.bat to find_java.bat. If I manually execute the command the batch file is trying to execute in command prompt, it works fine, but the batch file says it's wrong. Another suspicious thing seems to be that I cannot double click on ANY batch files in the computer. I can only open them as admin or from command prompt. Starting to think this is a very computer-specific problem.
If anybody else is seeing this, there's a real problem on your system when you can't launch a BAT file by double clicking it.
Something you could test on your machine is drop into a cmd shell then using the ftype and assoc builtins to check these are the defaults:
➜ ~\scoop\apps\android-sdk\current\tools\lib $ cmd
Microsoft Windows [Version 10.0.19041.572]
(c) 2020 Microsoft Corporation. All rights reserved.
C:\Users\cwalsh\scoop\apps\android-sdk\current\tools\lib>assoc .bat
.bat=batfile
C:\Users\cwalsh\scoop\apps\android-sdk\current\tools\lib>ftype batfile
batfile="%1" %*
If it's still broken you might need to dive into the registry and figure out why the .BAT file extension has a different program associated with it.
My first question in stack overflow... Kind of excited but still struggling in the problem.
Alright, My question is how to pass parameters from command line to a java program through makefile.
Honestly I don't really know wether my description is correct....... Cause I don't really know much about makefile... In my assignment, the description is that we must develop a Makefile for GNU make to build our program. For example, the command lines
make
mipsim -v < test1.cmd > test1.log
will build the ISS (a simulator we made) and then run it with debugging output, taking input commands from the file test1.cmd and writing result to test1.log.
I have finished the program but I don't know how to make the things above happen.
What I know so far is just to use makefile to make the .class file from .java file....
I have no idea about how to get test1.cmd as my input file's name and test1.log as my output
file's name from command lines.... I guess these two names probably will get into my program through String[] args in the main function...
Could anybody give me some help please?
Thanks
There is some confusion as to the issues.
First, compile Java using make is a little... iffy. (Most people use ant or maven.) However, if you don't mind a little overhead, you can do it using make. You probably should run make from a directory at the root of the Java package hierarchy. You can determine all Java files below using make macros. Hint: shell:
JAVA_FILES = $(shell find -name \*.java)
Then you run javac. (Make sure to define all path names to compilers etc. using make macros.) With Java, it's not easy to derive a make target, because .class files are not 1:1 w.r.t. java files. I just use a target "compile", depending on all the java files, and touch a file acting as a dummy target.
Second, the execution. To invoke a Java program that is not in an executable jar, you set the classpath (option -cp), specify the main class name and add command line parameters. I'd have to know what "mipsim" is - probably a shell script for doing just that. Anyway, a make target could be the log file:
%.log : %.cmd
${JAVA_HOME}/bin/java -cp ${ROOT} <$< >$#
Now, make test1.cmd should run your program.
Note: Redirection is not specified by program arguments; this is handled by the shell.
Quick comment on your question.
your makefile needs 2 targets. one for build and the other for the run.
all: build run
build:
(this is to build class file from your java source)
run:
put your java command line here like "java ..."
When you run "make", it will call "all" target. And all target will call "build" and "run" target, so just put one thing in one target and use the combinations.
Your java code.
Is your java takes input filename as argument or from stdin?
If you want to take input filename, then you can take it from args argument passed to your main(String[] args).
If you want to read from stdin then you can create a bufferedreader as below.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Hope this help.
(+1 please, if you like this answer)
There are a bunch of unrelated questions..
The syntax you are showing:
mipsim -v < test1.cmd > test1.log
would call an executable mipsim. Pass "-v" to the args[1]. Redirect test1.cmd to be the standrad input and test1.log be the standard output.
The output input redirection happens by the operating system so in c++ reading from std::cin will read from the file and writing to std::cout will write to test1.log
In java these will be redirected to System.in and System.out
About makefiles
Basically a make file rule looks like this:
<target>: <dependency1> .. < dependencyn>
~tab~ command
So just like it is possible to build a target that calls Javac. It is possible to build a target that calls Java.. and so you can build a test target and use it to execute any command you need
If you built a c++ executable then you can execute it from the makefile in the same way.
test: mipsim
mipsim -v < test1.cmd > test1.log
Your final question about pass parameter values to command line from make file do you mean something like this?
make PARA1=1 PARA2=ABC.c
You can use the parameters in your makefile..
test: mipsim
mipsim -v < $(INPUT_FILE) > $(OUTPUT_FILE)
I am using a .bat file, which is reliant on a .jar file. I have java (jdk/jre) installed on my system. If I run a command on this command line api via cmd (e.g. nscmd --help), everything works fine.
However, I want to run commands inside another program (Automise www.automise.com). If I do so, with the command line tools in another folder to Java, I get the infamous "java is not a recognised keyword...etc" error. If I put the command line tools in the same folder as Java, I get this error:
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
Program returned code : 1
How can I successfully run the command line tools?The tool is this: http://www.newservers.com/language/en/documentation/reference-nscmd.html
Thanks
Add the directory java is in to your path variable.