Cannot execute Java Classes on OSX - java

I'm trying to execute a java build using commandline in OSX 10.10,
The command I'm using:
java -classpath ./bin;./libs/log4j-1.2.17.jar example.hello.Server localhost:80
I've got MAMP up and running on the classPath and java is in the same "classes" directory.
Terminal is showing all options that can be used with java and is not executing the file. The same happends when using -cp.
Thanks

You have a misplaced ; instead of : in your classpath string. Try:
java -classpath ./bin:./libs/log4j-1.2.17.jar example.hello.Server localhost:80
Remember on OSX (or any other Unix flavor system) path separator is : not ; as on Windows/DOS

Related

How to detect java installation from a batch program

I want to run a jar file on a client machine using batch script. But the problem is its not clear where is the java installed. There are 3-4 fixed paths where java.exe can exist.
How to write a batch script to find where java is installed on any of those predetermined locations and then run a command to execute a jar file.
Assume java is not set in the environment variables and no access to windows registry.
On windows :
c:\> for %i in (java.exe) do #echo. %~$PATH:i
On linux:
$ which java

java command line compilation

I have a simple, single file java program that relies on a single static jar. The java code and the jar reside in the same directory. For this one-off solution I don't want to bring in the weight of ant or maven, and just want to compile it directly.
On my dev box, the following compiles and runs my code fine:
javac -cp ".;dependency.jar" File.java
java -cp ".;dependency.jar" File
However, on my test box, the java command fails, and I get the following output:
Error: Could not find or load main class File
If I change my classpath arg to -cp "." I get the following output:
Exception in thread "main" java.lang.ClassNotFoundException: dependency
My dev box is 64-bit Windows/Cygwin and java version 1.7.0_55. My test box is 64-bit Linux and java version 1.7.0_45.
What is going wrong on my test box?
The classpath separator character is different on Linux (and on Unix) than it is on Windows. It's ; on Windows, but it's : on Linux (and Unix).
Try this on Linux:
javac -cp ".:dependency.jar" File.java
java -cp ".:dependency.jar" File

Running java jars under linux

I created a jar file and want to run it on linux machine.
In win32 machine I'm using:
java -classpath myclass.jar;log4j-1.2.16.jar;mysql-connector-java-5.0.8-bin.jar;. com.name.myClass.MyClass
However, on linux its doesn't work? Any ideas how to do it?
Use : instead of ; to separate items in your classpath:
-classpath myclass.jar:log4j-1.2.16.jar:mysql-connector-java-5.0.8-bin.jar:.

Ubuntu System Start-up: What command to insert to run java app?

In Ubuntu 10.10, System/Preferences/Startup Applications, I am trying to add a .jar program. If the program sits in home/john/this-folder/app.jar, what would I put in the command line for it to run on start-up?
java -jar /home/john/this-folder/app.jar [optional arg if any]
java - the Java application launcher
Note that you need to include jar in classpath if you java app needs dependecies.
You do that by:
java -jar /yourApp.jar -cp /home/zzz/libs/

How do I run a java program from a different directory?

I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
This is my script:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
produces the same results.
add your directory to classpath example:
java -classpath commandprogram CommandProgram
or
java -classpath directory_to_program Program
After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:
#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."
I had previously encountered this problem and solved it with the cygpath -w solution, but then changed my script slightly and didn't notice that the path problem came back.
you have to use a dot to separate packages, not a slash.
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram
The usual way of running a java file is to save it in the Java/Bin folder and Run cmd
C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname
If you save the file in different directory such as D:, you can use the following on the cmd prompt:
D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin

Categories

Resources