I would like to use a run configuration (or something similar) to run a class from Eclipse using a shell script. The shell script will do a bunch of fancy stuff to make the job run on a machine with more memory. That part I can do.
What I don't know is how to make Eclipse pass the class name and class path to a shell script - and ideally show the output of the shell script in the console window. This seems like it should be simple. I'm using 3.5.2.
Thanks!
I'm not sure of a clever way to pass your classpath to your script, but the External Tools Configurations can do this. And the output will be printed in your console. There are Variables that can be set up to manage your classpath a bit better than I have done below. Though I've used the working directory as my classes folder, so I only need to use . as my classpath.
Variables can be used throughout to set up common paths etc.
Related
I'm building a desktop app that uses Cygwin to execute shell scripts on Windows 10. Ideally, users wouldn't have to install Cygwin because I'm putting the relevant exe files in my project. I have Cygwin's "sh.exe" in my project which I can call without an absolute path and it seems to be running grep, zcat, awk, etc with no issues.
It looks like I'm running into the issue explained in https://www.question-defense.com/2010/08/25/windows-7-cygwin-sort-input-file-specified-two-times where Window's cmd is using it's own sort rather than Cygwin's sort.exe and you have to put the path of the sort.exe in the script. So it looks like the user would have to download Cygwin which would somewhat defeats the purpose of my project because I want a hassle-free experience for users. I don't want them to have to download dependencies.
The only resolution I've thought of is to include the sort.exe in the project and replace the "sort" string in the script with the Windows path of the sort.exe, which I think would be in the directory where the user launched the app; maybe System.getProperty("user.dir").
Is there a better solution?
MobaXTerm seems to have got it down. They have a CygUtils plugin, which you have to download and put in the right place, but I imagine it wouldn't be hard for them to have their application come with CygUtils. I'm trying to do something like that.
I faced same issue while using Cygwin.
I renamed [cygwin64_folder]\bin\sort file to csort (or any other convenient name if you wish) and used csort command for my usage.
My java application uses environment variables (System.getenv), and this set of variables is large and defined in a file like:
export VAR1="VAL1"
...
export VAR100500="VAL1500"
when i start the application from command line i use something like this:
source ../.config/.secret-secret && java -jar build/libs/app-name.jar
and the values are exported right before the launch (actually, the source command may be executed only once per session, since the values are exported)
But when i start the application from IDEA, reasonably those values are not exported to the run environment.
So, the question is: how can i export a set of environment variables to application launch environment in IDEA?
So, what i've tried already:
in Run/Debug Configurations -> Before Launch i started a shell
script which exports those variables (i use the same script to run
the app from command line). Script executed successfully, but looks
like the script is executed in different environment from the
application itself, so values are exported (and printed to IDEA run
console), but the application still can't read them from
System.getenv
i can't easily use Run/Debug Configurations -> Environment Variables, because there are a lot of variables to set up manually, also they are changed from time to time.
It's not enough to run entire IDEA with those variables, because, as i mentioned above, the values are changed often and should be re-read almost every launch.
I can tell you that editing the workspace.xml file (which is where the environment variables entered in the run configuration are stored) directly does not work, so don't bother trying that.
I think you are going to have to write a plugin that parses your script and updates the run configuration. I haven't written a plugin to do this myself so can't give you a fuller answer. You can see the relevant plugin in documentation here
If I'm trying to run a java program and I don't know the exact name of the Main class, is there any way to use tab completion to figure it out?
java -cp stackoverflow.jar org.<tab>
stackoverflow serverfault stackexchange
java -cp stackoverflow.jjar org.stackoverflow.<tab>
Main IntegrationTest QuestionAnswerConsole
Something along those lines.
Basically you are asking how to configure shell autocompetion to support java. It is possible. Take a look on this discussion: How does bash tab completion work?
I have to say that this is a good idea not only for discovering the main class but also to complete other command line options and a class path. I'd be glad to use such script if you develop it. Good luck.
EDIT
At least on my Ubuntu file less /usr/share/bash-completion/completions/java exists and therefore some completion should work. You are always welcome to improve the script.
The only way to get the Tab Completion that you are talking about is to use a shell that is "Java aware" or a shell script that provides this feature for the java command. git has a similar feature, so I don't think it's completely impossible.
Edit:
According to this question on SU, it is possible to create an autocompletion script for the bash shell. Since the question on SU is slightly different than what you are asking, I don't see a lot of specific details that relate to this question. However, it looks like a good place to start.
I don't know any easy way to get the autocompletion of classes names if they are hidden in a jar file.
On the other hand, you can add a Manifest to your jar to make it auto-executable (ie., you just have to run java -cp ... -jar stackoverflow.jar) !
In the jar archive, add a META-INF folder, and inside that folder, create a MANIFEST.MF file that reads :
Main-Class: org.stackoverflow.Main
(or whatever your main class really is).
Here is some documentation : Setting an application's Entrypoint
Other answers suggest you solutions but as a sidenote, you don't need it if you provide maintainer of the jar file provides a manifest file with path META-INF/MANIFEST.MF. Then java automatically extracts main class from the manifest and you can run it this way:
java -jar stackoverflow.jar
I want to run a jar file in both unix and windows without have to call it directly with java like:
java -jar myjar.jar parameters
i want :
myjar.jar parameters
I've been reading allready -
Running a JAR file without directly calling `java`
Which seems like a very nice hack for unix .
Howerver , this wont work in windows.
I'm looking for a uniform solution that will work both on unix and windows , but I'm not sure there is such.
The solution has to be only once , and it has to include changes related to the jar only ,and not the operation systems - because this is a file to I'm suppling to a client.
What you are asking can't be done: Windows will load executable files only in the PE/COFF format used in .exe in .dll files.
What you can do instead is supply the users a "wrapper" program that starts the actual Java program. You could create the wrapper in C, which has several benefits: you can set an icon on the executable and associate the program with file types in the Windows Explorer. Batch files are a popular alternative; they are easier to create.
You can provide a script that starts your application for both Unix (.sh) and Windows (.bat). This seems to be the preferred approach for many companies. An example would be JBoss Server where a run.bat is provided for Windows and a run.sh is for Unix. These scripts set the appropriate environment variables, classpath, etc and then call java.
You can write your own bash and batch/powershell scripts. As for windows, you can try Launch4J. It should be easier than writing elaborate scripts from scratch.
Be aware that you can only provide wrappers to make the execution simpler (one click). Java has to be run anyway. Either explicitly, by the user, or as part of a script. You can't do without it.
I am running a Java application from the command line. Can I specify a command line argument to set the current running directory to something other than where the application is actually going to run?
There is a JVM argument -Duser.dir which can be used to set working directory for JVM.
If it all possible I would rather use a script to run the java application and set the directory in the script:
#!/bin/sh
cd <your dir>
java <some arguments>
The JNI-solution may affect all kinds of relative paths in your application; for examples the classpath you put in.
If you want to change the current directory, you'll have to use JNI and invoke a native API from your Java code. For example, for Windows you would use SetCurrentDirectory
I found this SO post and it helped me solve my problem. Though I wanted to share something specific about IntelliJ that might trip somebody else up.
I have posted a picture below where the -Duser.dir flag is used and also the Working Directory text field is filled in.
In this situation, the Working Directory will be set to "JarLearning" rather than "2ndTry".