Foreman cannot find $JAVA_OPTS - java

I have an error following the next tutorial from Heroku specifically on this part
[https://devcenter.heroku.com/articles/getting-started-with-java#run-the-app-locally][1]
If I execute that instruction foreman throws the following error:
Error: cannot find java class $JAVA_OPTS
I have already declared a env variable like this:
Name variable : JAVA_OPTS
Variable value: -Xms256m -Xmx512m
The Proc file that foreman is trying to execute has the following:
web: java $JAVA_OPTS -cp target/classes:target/dependency/* Main
Im clueless about what is happening.
Note: I already checked some other questions
Running java with JAVA_OPTS env variable
Foreman terminates immediately
foreman can't find java
Hope someone knwos what is happening.
EDIT : I answered my own question below

I suspect you are running on Windows. If so, then you'll have to reference the JAVA_OPTS var like %JAVA_OPTS%. But Heroku will still need the *nix style ($JAVA_OPTS), so I recommend creating a Procfile.win next to your Procfile with the following contents:
web: java %JAVA_OPTS% -cp target/classes:target/dependency/* Main
Then run this to start your app locally:
$ foreman start --procfile=Procfile.win

This is what I did in order to solve the issue:
Seems that the documentation at heroku site is not clear about what operating system are you using. But then I found the answer in the link below:
Heroku Deploy your Java app locally
The original Proc file script whas like this:
web: java $JAVA_OPTS -cp target/classes:target/dependency/* Main
and I modified it as you can see below:
web: java %JAVA_OPTS% -cp target\classes;"target\dependency\*" Main
Just as Heroku documentation states.
This solved the problem and I was able to run my app locally

If you're using 'nix,
export JAVA_OPTS
before running the script that expects it.

Related

How to run jar files sequentially from a shell script

I am trying to run two java application one after other in my docker container.
In my dockerfile i have specified invoker.sh as the entry point.
ENTRYPOINT ["sh", "/opt/invoker.sh"]
Then i use this script to run two jar files.
#!/bin/sh
java -jar loader.jar
java -jar service.jar
but this does not work. It gives
Error: Unable to access jarfile javaimpl-loader.jar
and only the service.jar is executed. When i tried echo $(ls) it shows that both the jar files are there.
but if i change the script to
#!/bin/sh
echo $(java -jar loader.jar)
java -jar service.jar
then both the jars work. Why cant i use the 1st script. any help regarding this highly apreciated.
It appears the first example is being treated as a single line, you could work with that. Also, I would prefer bash to /bin/sh. Like,
#!/usr/bin/env bash
java -jar loader.jar && java -jar service.jar

Error trying to run headless BehaviorSpace

I feel I must apologize for such a basic question, but I am getting an error simply trying to run BehaviorSpace experiments in headless mode. I tried running my own model experiments from the command line, but got an error. So I then tried following the exact instructions on the BehaviorSpace documentation. To do this, I created a BehaviorSpace experiment in the Fire.nlogo model called "experiment1" (see screen shot) and then tried to execute commands to run experiment1 from the command line. The screen shot of the terminal shows that I first set the directory where I have NetLogo 5.3 installed, and then tried to run the commands from the BehaviorSpace documentation. The screen shot of the terminal also shows the Java error I am getting. I have never used the terminal before and am not sure what I am doing wrong, but I am sure I am missing something simple.
I am using Mac OS X and NetLogo 5.3. Thank you for your time.
Seems you're not working in the correct directory.
You need to cd into the netlogo directory:
For me:
netlogo_directory = "/Applications/NetLogo 5.2"
so
cd /Applications/NetLogo\ 5.2
Then you can execute your command:
java -Xmx2048m -Dfile.encoding=UTF-8 -cp ./Netlogo.jar org.nlogo.headless.Main --model /path/to/your/file/name/filename.nlogo --experiment experimentname --table /path/to/log/with/filename.csv --spreadsheet /path/tp/spreadsheet/with/filename.csv
The problem is that the Java file that comes with NetLogo is where the .jar file and lib file are located. Hence, a simple addition of Java/ in the below code allows all files to be found.
java -Xmx1024m -Dfile.encoding=UTF-8 -cp Java/NetLogo.jar \
org.nlogo.headless.Main \
--model Fire.nlogo \
--experiment experiment1 \
--table mytable.csv

Java not picking up on environmental variable

I'm trying to set an environmental variable in linux. I followed the instructions here: Make $JAVA_HOME easily changable in Ubuntu
Despite using source /etc/environment and using echo MY_VAR to verify that linux detects the variable, my java app will not pick up on it. The variable continues to return null
public static void main(String[] args) throws IOException {
System.out.print(System.getenv("MY_VAR"));
I'm executing my java application via sudo java -jar /path/to/my.jar
Update: My mistake, I hadn't included the correct command. I'm actually sudoing.
You need to export the variable
export MY_VAR=stackoverflow
java -jar /path/to/my.jar
then you can print the value with
System.out.print(System.getenv("MY_VAR"));
edit: short example script (amend the path if necessary)
#!/bin/sh
MY_VAR="foobaz not exported"
echo "MY_VAR: ${MY_VAR}"
java -jar my.jar
export MY_VAR="foobaz exported"
echo "MY_VAR: ${MY_VAR}"
java -jar my.jar
from the linked answer ..
Execute "source /etc/environment" in every shell where you want the variables to be updated:
When you call java -jar /path/to/my.jar I think you will be starting a new shell, meaning that the contents of ./etc/environment wont be available to the shell your java code is running in.
try
export MY_ENVIRONMENT="HELLO"
java -jar /path/to/my.jar
Does that look any better?
And if you are sudo -ing your command ...
sudo -c export MY_ENVIRONMENT="HELLO";java -jar /path/to/my.jar
or something along those lines
The issue is that sudo runs with it's own set of environment variables. You can either add the variable to the root environment (sometimes finicky), or tell sudo to maintain the current environment's variables explicitly.
Here's how you would do the latter:
Open up your bash profile
vim ~/.bash_profile
Add the environment variable to the file export MY_VAR=80
Open up the sudoers config file sudo visudo
Add the following line to the file exactly as so Defaults env_keep +="MY_VAR"
Now when you run sudo java -jar /path/to/my.jar it should work as desired, with MY_VAR set to 80.

java running jar via cronjob

I want to setup a Minecraft server, which automatically starts up on system startup.
I use the following script to manually run: /home/mc_ftb/server/start.sh
#! /bin/sh
java -Xms1G -Xmx3G -jar /home/mc_ftb/server/mcpc-plus-1.5.2-R0.2-forge716-B527.jar nogui
The cron to start it on start up looks like that:
mc_ftb Ja ~/server/start.sh
This structure already worked with other mods, like Tekkit, but now with a FTB (NewWorld) mod, I get the error
Exception in thread "main" java.lang.NullPointerException
at cpw.mods.fml.relauncher.FMLRelaunchLog.resetLoggingHandlers(FMLRelaunchLog.java:212)
at cpw.mods.fml.relauncher.FMLRelaunchLog.configureLogging(FMLRelaunchLog.java:191)
at cpw.mods.fml.relauncher.FMLRelaunchLog.log(FMLRelaunchLog.java:242)
at cpw.mods.fml.relauncher.FMLRelaunchLog.info(FMLRelaunchLog.java:274)
at cpw.mods.fml.relauncher.FMLRelauncher.setupHome(FMLRelauncher.java:164)
at cpw.mods.fml.relauncher.FMLRelauncher.relaunchServer(FMLRelauncher.java:147)
at cpw.mods.fml.relauncher.FMLRelauncher.handleServerRelaunch(FMLRelauncher.java:45)
at net.minecraft.server.MinecraftServer.main(MinecraftServer.java:1622)
at org.bukkit.craftbukkit.Main.main(Main.java:21)
Starting the script in a terminal as mc_ftb works fine, but even not with the cronjob, I already tried to start it via exce java ....
The script and the jar file are +x'ed. I'm using the actual stable Debian.
Try adding a 'source /etc/profile' as the first line in your script, this will load the environment variables first.
So replace
#! /bin/sh
java -Xms1G -Xmx3G -jar /home/mc_ftb/server/mcpc-plus-1.5.2-R0.2-forge716-B527.jar
with
#! /bin/sh
source /etc/profile
java -Xms1G -Xmx3G -jar /home/mc_ftb/server/mcpc-plus-1.5.2-R0.2-forge716-B527.jar

foreman can't find java

I'm trying to use foreman to run a java app locally which deploys fine to heroku.
My Procfile looks like this
web: java $JAVA_OPTS -cp target/classes;target/dependency/* Start
but when i do formeman start i get
15:51:21 web.1 | unknown command: java $JAVA_OPTS -cp target/classes;target/dependency/* Start
If I just enter java at the prompt i get java's help text back so its on my path. If I use the full path to the java executable in the Procfile it works, but it'd be cleaner if I didn't need two versions of the Procfile
Is there somethinf funky going on with my path or is foreman not getting the path from my environment?
Could it be the environment variable is the problem?
Windows:
java %JAVA_OPTS% -cp target/classes;target/dependency/* Start
Linux:
java $JAVA_OPTS -cp target/classes:target/dependency/* Start
Looks like the example is a mix of both :-)
Environment variable

Categories

Resources