I am trying to run a perl command with Java runtime exec in linux/ubuntu/gnome. The command generates an pdf file, but it saves it in my home folder. Is there any way that the exec method can set an output path for the commands executed? Thanks in advance.
The exec method just runs the command on the operating system, so you'll want to change the command you're running with exec more than anything in "Java" per se.
There are a few possibilities:
Change the working directory of your java program. The .pdf is being saved in your working directory because this is where the program is being run.
Unfortunately it's not simple to change this value after the program has been launched. It is, however, trivial to change before the program starts; just change the working directory before starting the program.
Move the file to it's desired location after it's been created in your home directory.
Change the command so that it includes the target location. Your perl script may have an option that will enable you to save it's output to a certain location (usually -o or --output). Using this your program would change from:
Runtime.exec("perl someprogram");
to something like:
Runtime.exec("perl someprogram -o /path/to/some.file")
You might be able to use "output redirection", if there is no option to do this.
Try something like what's below as your argument:
Runtime.exec("perl someprogram > /path/to/some.file")
Unfortunately, without knowing more details of your situation I can't provide more concrete advice.
While each approach has benefits and drawbacks, it's probably best to just implement the one that you understand best; if you can't get one to work, try another.
A good, free online resource for learning is Introduction to Linux: A Hands On Guide.
Section 2.2 has details on cd which you can use for 1..
Section 3.3, section 3 teaches about the mv command, which will be useful in 2..
Section 5.1 is about I/O redirection. Knowing about "output redirection" and the > operator, are important for 4..
For 3., you'll have to consult the documentation of the perl program you're using.
You could modify the Perl script to accept an absolute path for the output.
You can trying setting the working directory using exec(java.lang.String[], java.lang.String[], java.io.File) where File is the directory the command is executed from.
If all else fails, you'll can always copy the generated file from the Home directory to your final location.
Related
it's a good doc for people who first use java
but meet about command line i have a few questions
https://lift.cs.princeton.edu/java/windows/
His terminal code like this:
~/Desktop/hello> ls
Barnsley.java COS 126.iml WELCOME.txt logo.png
~/Desktop/hello> javac-introcs Barnsley.java
~/Desktop/hello> java-introcs Barnsley 10000
but in my idea,it uses cmd.exe so ls should be replaced dir i know
but when i type javac-introcs Barnsley.java
it tells me
'java-introcs' is not an internal or external command, nor is it a runnable program
Or a batch file.
enter image description here
The output you observed means that you haven't completed the installation of some class-specific programs. The installer for this is mentioned in Section 0 of the link you provided. The most likely cause is that there were some environmental variables that were not modified correctly to add the java-introcs executable or alias to %PATH%. However, you are probably able to replicate the intent of java-introcs, as described below.
To figure out how to make the given Barnsley file compile and run, you'll need to add the dependency StdDraw.class to the classpath when running the file. To do this, you can use the java and javac option -classpath or -cp. You can read more detailed documentation on how to do this here.
Hello again Stack Overflow!
I am currently working on a Java program that is essentially a digitized character sheet for a Dungeons and Dragons style adventure. I'm currently learning how to use IntelliJ IDEA, and while I haven't been able to figure out how to create a standalone executable .jar file, I have been able to find a workaround in the form of a .bat file that I have nicknamed rubberglove.bat (because if you can't open a jar, you use a...).
However, as in my previous post, I've run into a problem, as one of my new players uses a Mac, and since I don't know if it's possible to run rubberglove.bat in a non-Windows environment, I'll probably have to translate it into something MacOS can understand. However, I've never owned a Mac, so I'm not sure what the file extension even is, let alone what to put inside.
The contents of rubberglove.bat are shown below:
java -jar [program_name].jar
What would the Mac equivalent of this file and the commands inside? Thanks in advance for all your help!
Create a file called rubberglove (or rubberglove.sh, the file suffix is optional). And then set the execute bit. Something like
$ cat << EOF > rubberglove
#!/usr/bin/env bash
java -jar [program_name].jar
EOF
$ chmod +x rubberglove
$ ./rubberglove
Error: Unable to access jarfile [program_name].jar
Adjust "[program_name]" as needed.
I want my directory to give executable permissions (by default) to all the shell scripts which are going to be created in that directory after I run my Java program. I tried the following commands:
setfacl -d -m group:name:rwx /path/to/your/dir
find ./ -name "*.sh" -exec chmod +x {} \;
The first one is not showing any response while the second one works fine but I have to execute this command manually in terminal after my Java program has created all the scripts. This is not what i seek. I want this thing to be automatic. Here is what I am trying to achieve:
My Java program creates the .sh files in a directory.
Now the program would try to execute this script file.
Here is a Java code snippet which shows how it is going to execute the script files:
ExecuteShellComand obj = new ExecuteShellComand();
String command2 = "./script.sh";
String output2 = obj.executeCommand(command2);
It doesn't run unless I give the executable permissions to the script.sh. How do I do it? Is there any way around it? If I am not doing something in a way it should be done, feel free to give your suggestions. Thanks
Default ACL permissions are masked by the file's creation mode as specified in open and mkdir syscalls.
Since files are usually created with a default mode of 0666, execute permissions will be masked instead of inherited.
It's the responsibility of the program that creates the files to create them with the right permissions. If you set the permissions correctly when creating the scripts, you won't need ACL or chmod at all. The best way to fix this would be for your program to set the mode in the open call to 0777.
Java appears to have Files.createFile for this. Additionally, you have a more fuzzy File.setExecutable to do it after the fact, which is not Unix canonical behavior, but probably fine for your use case.
I have a Main.java file and I want to run the program passing it test.txt
I know in command line I can write javac Main.java
After compiling I can write java Main test.txt and this will accomplish running the file and passing test.txt
If I wanted instead to be able to just write main test.txt and have that trigger my Main.class file to run is that possible and if so how?
(Edit: Based on your comment, let me expand to add a couple more situations)
If your goal is to have someone else run your program who does not have Java installed, and you do not wish to have them install a Java runtime environment before running your app, what you need is a program that converts the .class or .jar files into a native executable for the platform you are using. How to do this has been covered in other questions, eg: Compiling a java program into an executable . Essentially, you use a program like JCG (GNU Compiler for Java) or Excelsior JET (a commercial product) to expand the byte code into full native code with a mini-JRE built in.
If your goal is to save typing, there are a number of strategies. Others have suggested alias commands, which work well on linux.
A slightly more portable option that you could ship with your program would be a shell script. Granted, shell scripts only run on linux or other OS's with shell script interpreters installed.
Here is an example shell script. You paste this into a text editor and save it as main with no extensio. The $1 passes the parameter argument fyi.
#!/bin/sh
java Main $1
presuming you name your shell script just "main" with no extension, you could call main test.txt to execute your program now.
If you are on Windows, you might want to create a windows shortcut, and point the shortcut to "java Main test.text", using the full paths if necessary (if the paths are not already set). Of course, this does not make the parameter easy to change every time you run it, you would have to edit the shortcut.
add an alias
e.g. under a mac edit your .bash_profile with the following line
alias main='java main'
don't forget to open a new console to see your alias working
Depends on your operating system. On Linux with the bash shell, for instance, you can set up an alias to expand your main into java -cp myjar.jar main.
Linux can also be configured to 'understand' Java class flies as a binary format directly see here (linux kernel documentation).
If you're on windows, you'll have to wait for answer from someone with more knowledge about that than I.
Good luck!
I'm using gvim as my main 'IDE' on windows 7 and I would like to use ctags to navigate through the code. I've downloaded it and ran based on this tutorial: http://www.techrepublic.com/article/configure-vi-for-java-application-development/5054618
ctags -f ~/.tags -R ~/myprojects/src $JAVA_HOME/src
I've then setup my vimrc with...
set tags=~/.tags
However when I do Ctrl+] on a keyword, it says it can not find the file which the tag is defined in. Shows the correct path except it misses out c:\ from the start so vim can't load it.
How can get it to give me the correct path?
I'm using the latest version of gvim and ctags.
Thanks
FWITW, I'm not entirely sold on the concept of keeping my tags in one location.
This part of the command call:
-f ~/.tags
Nor would I hard path to my current project. This part:
-R ~/myprojects/src
BTW, Windows doesn't have ~ so I don't think either of those would work (not sure if Vim will find ~, i.e. "home").
If I were you, I would cut my teeth on the simplest method until you get more comfortable with the Vim methods and ideals.
Easiest method:
Always let Vim know the "Current Directory" (making the assumption that you are not launching Vim via the command prompt). When you open a file always set the current directory by issuing the following command from normal mode:
:cd %:p:h
Generate a tag file in the current directory with the following command (from normal mode).
:!ctags -R .
Happy jumping.