I ran the Mac installer here:
http://algs4.cs.princeton.edu/code/
which says
The Mac OS X installer downloads algs4.jar to the /Users/username/algs4 folder; adds it to the DrJava classpath; and provides the wrapper scripts javac-algs4 and java-algs4, which classpath in algs4.jar, for use in the Terminal.
I don't see an algs4 folder created in Users/username..not sure whats going on here.
Q. What is the easiest way to execute the main() method in classes that are contained in algs4.jar?
A. If you used our autoinstaller, you can execute with a command like
% java-algs4 edu.princeton.cs.algs4.StdDraw
This command works:
java-algs4 edu.princeton.cs.algs4.StdDraw
but when i try to run
java-algs4 edu.princeton.cs.algs4.Counter
or
java-algs4 edu.princeton.cs.algs4.BinarySearch
I just get errors like:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at edu.princeton.cs.algs4.BinarySearch.main(BinarySearch.java:94)
Try running with command line arguments that are required as input for this program.
java-algs4 edu.princeton.cs.algs4.BinarySearch largeW.txt < largeT.txt
Get the files from the downloaded data as part of your installation.
You can also refer to this link
Related
I am sure this is a stupid question and it must have been asked by every java programmer before. But I cannot find a related question at all.
This talks about subdirectories but I don't have any subdirectories as they are all in the same directory as the java file and the directory I executed the command line from Executable jar file error
This solution gives me the same error as I am writing below: Java command line with external .jar
Others (I don't have links to) talk about Eclipse and other IDE but I am not using an IDE, just a Linux terminal.
I am trying to import a public jar file from http://www.hummeling.com/IF97. The downloaded jar file has been renamed to if97.jar.
I have a java file called steam.java with these commands inside the file:
'
import com.hummeling.if97.IF97;
IF97 H2O = new IF97(IF97.UnitSystem.ENGINEERING);
System.out.println("test H2O table PSpecificEnthalpy(1): "+H2O.specificEnthalpyPT(1,300));
System.out.println("test H2O table PSpecificEnthalpy(5): "+H2O.specificEnthalpyPT(5,300));
'
But I do not know how to run this file in the command line.
I successfully compiled by typing:
'javac -cp if97.jar ~/test/steam.java'
Now I have a file called steam.class
But when I execute it with:
'java steam -cp if97.jar'
or
'java steam -jar if97.jar'
I get error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/hummeling/if97/IF97
at steam.start(steam.java:364)
at steam.main(steam.java:341)
Caused by: java.lang.ClassNotFoundException: com.hummeling.if97.IF97
I am trying to execute this in Linux Ubuntu 16.04 using Terminal. Both the files (steam.java and if97.jar) are in the same Home directory where I execute the javac & java command on.
I believe (or I'm mistaken) that the problem is that java isn't able to find the jar file. But I don't know why.
Please advise, thank you in advance.
You need to specify the class name after the JVM options, because whatever coming after the class name are considered arguments for the class, not the JVM.
Try this:
'java -cp if97.jar steam'
So, I have removed openjdk from my new Ubuntu system and have installed Oracle JDK 8 and Eclipse from their respective websites. I can run a program from Eclipse, however I cannot run it from the command line. I am also not used to using Eclipse (I use NetBeans for my Java class in college.) I noticed that there is no build button in Eclipse. With all of that being said, here is my command line code:
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ ls
test.class test.java
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java wiltest.test.java
Error: Could not find or load main class wiltest.test.java
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java wiltest.testError: Could not find or load main class wiltest.test
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java test
Error: Could not find or load main class test
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$
You need to add the -classpath . command line option.
You can learn more about classpaths here
I believe the command java -classpath . test might work, but it really depends on a number of items that are better explained in the link above.
I actually answered part of my question myself. (For anybody wondering, the classpath is set to be in present working directory by default.) Anyways, I was running java wiltest.test from src and not bin. However, I ran from binary file and it worked. BUT I deleted the .class file thinking that I could change the source file, recompile using javac, and it would create another test.class in the binary folder. It did not. -sigh-
I am trying to run a Java program by using jsvc.
I have installed it by
sudo apt-get install jsvc.
To find out a solution, I tried to read the Apache documentation about it (at https://commons.apache.org/proper/commons-daemon/jsvc.html). But this command:
./jsvc -cp commons-daemon.jar:my.jar MyClass
and this other:
./jsvc -cp my.jar MyClass
did not work (of course, I replace the terms by the name of my class etc.).
It gives me the error:
bash: ./jsvc: no such file or directory of this type
So I use jsvc without "./". And I saw here: How to start tomcat with jsvc? that I should use /usr/bin/jsvc
But an other problem is when I use
/usr/bin/jsvc -cp path/to/my/.jar path/to/my/class
nothing happens.
I try the link: How to convert a java program to daemon with jsvc?. But there is something I don’t understand: for the "CLASS =", have I to put a .Main file ? And do I have to put the extension name of the file (for the class and the .jar) ?
I decided to put the .java file which contains my main class (once I putted the .jar, then I didn’t). Then I copied the code, and when I write "esac" and pressed the enter key in the Ubuntu console, the console closed up, and then…nothing.
Has someone already encountered this ?
Are you sure your java installation is in /usr/java?
Beside this, in the second command there's the directory missing. You should do something like that:
export JAVA_HOME=path/to/java/home
./configure
If you don't know where your java installation is located, try this if you are on a mac/*nix, or this if you have windows.
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.
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!