Java code not working in OSX Mavericks - java

I'm trying to run a java library, so that I can build upon it and do my customization. The library is called jayu parses ASN files so that you can decode them. It can be downloaded here
There are a few test data to check the library in the "test" folder and mentioned in the Readme.txt file. There is a asn2csv batch file for windows but I'm using OSX mavericks. According to the Readme file, I need to run it by invoking the command:
ls $ASN_DATA_DIR/*.dat | xargs java -cp "./*.jar:." Path/To/Stream1.txt test.testdata.Stream1 $OUTPUT_DIR
OR
java -cp "*.jar;$PATH_TO_TEST_DIR" Stream1.txt test.testdata.Stream1 . Path/To/Stream1.dat
But whatever I try I always get Error: Could not find or load main class Stream1.txt
I'm not a Java programmer. What am I doing wrong here? The development of this seems to be inactive lately but it's still useful. So, I'm posting it here, hoping someone will help me run the example.
Edit: I've added the tree structure of the directory and the files
+ jayu
|--Readme.txt
|--commons-compiler.jar
|--janino.jar
|--jayu.jar (ASN parser)
|--AsnToCsv.bat (Command line Tool)
+--test (Contains test data for examples)
|
+ testdata
|
Stream1.txt (Grammar File)
Stream1.dat (ASN Data File)
Stream1.java {mapFile}
...

It is interpreting Stream1.txt as the Java class you are trying to execute, because it is treating at as your first argument to java. Your first argument should be the name of the class containing the main(), or, the executable JAR must be indicated with a -jar option.
This would seem to indicate that "*.jar;$PATH_TO_TEST_DIR" is evaluating to blank. Can you see if there are any .jar files in your current working directory? Also what is the value of $PATH_TO_TEST_DIR?
Another thing is that if you use the second form on OS X, you should have a : instead of a ; because it is a Unix-based OS, not Windows.
Update1
I had a slight error with my description of how to run an executable JAR. You use the -jar option, not -cp (I corrected it above). Since they put -cp in their invocation, I'm guessing they are not intending to target the executable JAR, but rather to name the main class. That to me says that test.testdata.Stream1 is that main class, which means the ordering they gave you is wrong. Try this:
java -cp "*.jar:$PATH_TO_TEST_DIR" test.testdata.Stream1 Stream1.txt . Path/To/Stream1.dat
or some other ordering that starts with:
java -cp "*.jar:$PATH_TO_TEST_DIR" test.testdata.Stream1 ..........
That is, that makes test.testdata.Stream1 the very first command line argument to java.

Related

Translating a Jar-opening Batch file into its MacOS Equivalent

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.

Access classes defined in a jar file, from within (for instance) JRuby irb?

(Crossposting note: I have asked this question one week ago at the JRuby mailing list, but didn't get any answer yet).
I have a jar file provided by someone else, no access to the source code. The jar file is in lib/other/appl.jar, the class is named Appl, and the package is com.abc.xyz
I would like to instantiate an Appl object from the JRuby irb, jirb_swing_ex.
(Of course my problem applies not only to jirb, but to running JRuby programs in general, but I explain it in the way I'm using it right now, just in case there are some peculiarities in Jirb which need special treatment).
THIS is the way it DOES work:
(1) Invoke jirb by:
java -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex
(2) Put the directory with the jar file into the load path:
$: << 'lib/other'
(3) Load the jar file
require 'appl.jar'
(4) Import the class
java_import com.abc.xyz.Appl
(5) Create the object
x = Appl.new
As I said, this works, and I can live with it if necessary, but I would prefer a simpler approach:
NOW TO MY QUESTION: Instead of fiddling around with load path and doing a require for the Jar file, I thought I could let Java already include the jar file. This is what I have tried:
java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 jb/jirb_swing_ex
The problem is: How can I get at my object? If I just use the class name com.abc.xyz.Appl, JRuby complains that the class not found (NameError: missing class name).
BTW, I have also tried forward slashes (since I'm on Windows), i.e.
java -cp lib\other\appl.jar -jar jr\jruby-complete-1.7.27 jb\jirb_swing_ex
but the same effect. I had expected that, having appl.jar in my class path, would make the classes available somehow, but I seem to miss something.
Running jirb or jirb_swing with custom class path
jirb and jirb_swing will use the value of JRUBY_CP environment variable (if present) to extend class path given to Java command line.
Example with commons-lang3 library taken from my local maven repository, using bash on Linux or macOS:
$ export JRUBY_CP=${HOME}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar
$ jirb
irb(main):001:0> org.apache.commons.lang3.mutable.MutableBoolean.new
=> #<Java::OrgApacheCommonsLang3Mutable::MutableBoolean:0x7c24b813>
Running JRuby programs with custom class path
To run a JRuby program that uses a third-party java library, this won't work:
java -cp lib/other/appl.jar -jar jr/jruby-complete-1.7.27 ...
You must use either -jar or -cp, you can't combine the two.
From java man page:
When you use this option [-jar], the JAR file is the source of all user classes, and other user class path settings are ignored.
In addition, you need to pass the main Java class, which is org.jruby.Main, and that class needs arguments: either a path to a Ruby script, or other command line arguments such as -e 'puts 2+2'.
So the command line structure is the following:
# Run script file:
java -cp path/to/jruby.jar:other/custom.jar org.jruby.Main path/to/script
# Run simple one-line Ruby program
java -cp path/to/jruby.jar:other/custom.jar org.jruby.Main -e 'some ruby here'
(on Windows please use ; instead of : as separator)
Actual example with same commons-lang3 library & OS:
$ alias myjruby="java -cp ${JRUBY_HOME}/lib/jruby.jar:${HOME}/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar org.jruby.Main"
# Verifying base jruby code works with that:
$ myjruby -e 'puts 2+2'
4
# Now verifying it can use my 3rd-party lib:
$ myjruby -e 'puts org.apache.commons.lang3.mutable.MutableBoolean.new'
false

How do I add a .jar file to Rhino's classpath?

I am scripting Java via Rhino, and I would like to use the JCodec library. I put the jcodec-0.1.9.jar file in the same directory, and I added it via -cp on the command-line. The command to invoke Rhino looks like:
java -cp "rhino-1.7.7.1.jar;jcodec-0.1.9.jar;." org.mozilla.javascript.tools.shell.Main -opt 9 js/main.js %*
However, the library doesn't load at all, since all of its classes remain undefined. For example, running:
print(java.lang.Class.forName('org.jcodec.api.awt.SequenceEncoder'))
throws a ClassNotFoundException, while it doesn't for say, 'javax.swing.JFrame'.
I feel like I'm not including it correctly, but everywhere I've looked, this seems to be the way. Anyone have any suggestions?
try the following to make sure that the current directory is in your classpaht:
java -cp ".\rhino-1.7.7.1.jar;.\jcodec-0.1.9.jar;." org.mozilla.javascript.tools.shell
Note: if you're on UNIX change the .\ to ./ and the ; to :
UPDATED:
The class you are trying to reference does not have an 'awt' in it. Should be:
org.jcodec.api.SequenceEncoder

Running jar via command line : classnotfound exception on ONE desktop

I've a java script who's running by several user and working very well.
Today, I asked another user to try the script on his desktop and he's getting a ClassNotFoundException... despite the script is perfectly the same as mine (and jar locations is also the same)
Here's the command tu launch the JAR :
java -cp .;customname.jar;libs/* my.package.MyMainClass
And I also tried to add every jar in the libs folder separately :
java -cp .;customname.jar;libs/lib.jar;libs/lib2.jar;libs/lib3.jar my.package.MyMainClass
And here's the error message the user is getting :
Error: Could not find or load main class ch.vaudoise.hp.services.listener.AutoSysReorder
I checked the JAVA configuration and try to set him the same java version on "Path" environment variable. Same error.
As there's 6 user who can run the script and only one who's getting an error I'm sure it's a configuration issue. But what ? Classpath seems to be OK...
Many thanks for any help..
First things first: You must start by finding where is the conflicting class. If you don't know it, you may find it in two alternative ways:
Programatically: Code this class and execute it with the same classpath (on an environment that does not suffer the problem):
public static void main(String[] args)
{
System.out.println(ch.vaudoise.hp.services.listener.AutoSysReorder.class. getResource("/ch/vaudoise/hp/services/listener/AutoSysReorder.class"));
}
Manually, one by one: Open a command shell and execute:
javap -cp . ch.vaudoise.hp.services.listener.AutoSysReorder
javap -cp customname.jar ch.vaudoise.hp.services.listener.AutoSysReorder
javap -cp libs/lib.jar ch.vaudoise.hp.services.listener.AutoSysReorder
javap -cp libs/lib2.jar ch.vaudoise.hp.services.listener.AutoSysReorder
...
Try one by one every entry in the classpath until the class is found.
Once found the location of the class, open a shell in the conflicting PC and make sure that path is accessible:
dir lib\conflicting-library-or-directory
Also, repeat the javap test:
javap -cp conflicting-library-or-directory ch.vaudoise.hp.services.listener.AutoSysReorder
After this tests, you should have more clues to find the cause of the problem.
Take a look at ClassNotFoundException despite class in the classpath
You are also including meta character (*) in your classpath.
Try without that as suggested in the link.
Also some times copy pasting to command line , may get some characters copied differently.
I cannot add comment as of now : So , editing this answer.
| Java path is not an issue.
Try the following step by step :
- find which jar the class that is being not found is in .
- include only that jar as cp.
- include only that class and try
java -jar that.jar
Also try this once
java -cp "*;"
If you still got issue , probably the jar does not contain the class (You can open jar and check).
And you say script - is this single command which is failing or is it part of script ?. Using java -jar -cp , usually ignores cp.

Making a "macro" command to run a program

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!

Categories

Resources