Can't run java prorgram with "java" command directly - linux

I have compiled the java codes with
razrlele#OVO:~/workspace/javastudy/src$ javac Helloworld.java
and it turns out no errors or warnings
then I run the program with
razrlele#OVO:~/workspace/javastudy/src$ java Helloworld
it returns this
Error: Could not find or load main class Helloworld
I have to input like this
razrlele#OVO:~/workspace/javastudy/src$ java -cp ./ Helloworld
so that the program will run correctlly.
I am confused about why the "java" command doesn't work.
Here is my /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_11"
CLASSPATH=/usr/lib/jvm/jdk1.8.0_11
JAVA_HOME=/usr/lib/jvm/jdk1.8.0_11

Java will attempt to use your classpath to locate the class files. Since your classpath is set to /usr/lib/jvm/jdk1.8.0_11, that's where it looks.
By overriding the classpath with -cp ./, you tell it to look in the current directory for its class files.
There are a number of ways to fix this, including changing your classpath environment variable to include . or other localised paths as needed. I tend to prefer just setting up an alias (in my .bashrc for example) so regular Java programs aren't affected, something like (from memory):
alias jhere="java -cp $CLASSPATH:."
then I can just use:
jhere HelloWorld
to test my snippets.

Because . isn't in your CLASSPATH, first
export CLASSPATH="/usr/lib/jvm/jdk1.8.0_11:."
then
javac Helloworld.java
java Helloworld
It's also possible to use -cp (which is short for -classpath) with java at the command line, if you run java -h you will see
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A : separated list of directories, JAR archives,
and ZIP archives to search for class files.

Related

HaxeDevelop with hxml doesn't target the platform

My build.hxml file looks like this:
-main Main
-cp src
-js bin/index.js
I use js.Browser in Main class. When I try to build it with F8, FlashDevelop gives me "You cannot access the js package while targeting cross". And I actually see that it tries to run:
Running process: bla-bla-bla -target "js"
...
cmd: cmd /c haxe build.hxml
haxe -cp src -main Main
So it removes -js parameter from hxml and then fails the build. How to fix it?
In the project properties, choose "Custom Build" as compilation target.
The reason is that the hxml target uses a custom build command (Build tab) and when the Application compilation target is selected, FD will try to compile it a second time with an incorrect configuration. This is legitimately a bug in FD - raise an issue on Github?

Cygwin : error: package net.jradius.packet.attribute does not exist

This doesn't appear to be the usual CLASSPATH issue, but maybe something specific to cygwin.
The following illustrates the issue.
import net.jradius.packet.attribute.AttributeFactory;
public class Test{
static int x = 100;
}
When compiling the following error appears.
$ javac Test.java
Test.java:1: error: package net.jradius.packet.attribute does not exist
import net.jradius.packet.attribute.AttributeFactory;
^
1 error
The file jradius-core-1.1.4.jar is in the CLASSPATH.
When using the command line within Windows it works. Here is the classpath.
set CLASSPATH=c:\Temp\jradius-core-1.1.4.jar
When using cygwin it produces the error. Here is the classpath.
export CLASSPATH=/cygdrive/c/Temp/jradius-core-1.1.4.jar
The location is valid via cygwin as the following shows it is recognized.
ls $CLASSPATH
/cygdrive/c/Temp/jradius-core-1.1.4.jar
The same JDK is used for both scenarios.
Any idea why it may be failing via cygwin???
You are using JDK built for windows, so the environment is for windows.
That's being said, you should be provide windows-style path to javac.
cygpath is what you are looking for here.
Try:
javac Test.java -cp `cygpath.exe -w /cygdrive/c/Temp/jradius-core-1.1.4.jar`
cygpath -w means print in windows style.
Or just put the jar file in the same directory of the java file, and in cygwin:
javac Test.java -cp jradius-core-1.1.4.jar
Refer from this
Added the JAR files into the directory and changed the classpath to include '*' and that appears to have worked.

Class not found : helloworld.Main

I just began to learn Haxe, but I have encountered Compile error.
Main.hx
package helloworld;
import js.Lib;
class Main {
static function main() {
Lib.alert("Hello World");
}
}
Please be careful about the target class is helloworld.Main.
build.hxml
-js bin/HelloWorld.js
-cp src
-main helloworld.Main
-debug
Build process log
Building HelloWorld_p140627
Running Pre-Build Command Line...
cmd: C:\HaxeToolkit\haxe\haxe.exe X:\tmp\HelloWorld_p140627\build.hxml
Class not found : helloworld.Main
Build halted with errors.
Done(1)
Why? The class helloworld.Main is surely exist. I cannot even say "hello, world"?
Update now that I can see a screenshot of your project:
You are trying to compile "helloworld.Main", that means a class called "Main" in the package "helloworld", so Haxe will be looking for a file called "helloworld/Main.hx" in your "src/" directory.
However you have "src/Main.hx", not "src/helloworld/Main.hx". Create a subfolder called "helloworld", move "Main.hx" in there and you will be fine. The package you use in Haxe must match the directory structure.
Make sure your package aligns with your folders, and your file name with your class name. And all of these should be inside one of your "-cp" class path folders.
For your example above, the code looks fine, I would expect your layout to look like:
build.hxml <-- build file
src/helloworld/Main.hx <-- classpath/package/class.Hx
bin/ <-- output folder
bin/HelloWorld.js <-- will be created once it compiles
And then you'd run haxe build.hxml. If that doesn't work for you please post the exact file structure of your project (which folders and which directories), and the command you use to build it, and the output.
Hope that helps,

java.lang.IllegalArgumentException: Not a valid class name: ANDROID NDK javah

Im learning to code Android in NDK on WINDOWS Eclipse.... been following a Tutorial Book by Sylvain Ratabouil. So im in Run | External Tools | External Tools Configurations…
creating a new program configuration.
Name: MyProject javah
Location : ${env_var:JAVA_HOME}\bin\javah.exe
Working directory: ${workspace_loc:/MyProject/bin}
The problem comes In arguments...
when i try
Arguments: –d ${workspace_loc:/MyProject/jni} com.myproject.MyActivity
as it says in the book
i get when i click run
Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class name: –d
when i try ${workspace_loc:/MyProject/jni} com.myproject.MyActivity}
i get
Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class name: C:\Paul\Workspace\MyProject\jni
UPDATE:
i put -classpath first and it's running but can not find the class file for com.myproject
after further research....
i did it.
i set arguments to
-d ${workspace_loc:/MyProject/jni} -classpath C:\Paul\android-sdk-windows\platforms\android-17\android.jar;${workspace_loc:/MyProject/bin/classes} com.myproject.MainActivity
I also spend some long time on this problem when working with the the mentioned book about Android NDK.
Please note the following:
The java classname is NOT written in { }, just write something like com.myproject.MyActivity
The -classpath parameter can receive several paths devided by semicolon.
In my case this parameters worked:
Location:
${env_var:JAVA_HOME}\bin\javah.exe
Working Directory:
${workspace_loc:/myproject/bin}
Arguments:
-d ${workspace_loc:/myproject/jni} -classpath ${workspace_loc:/myproject/bin/classes};"C:\Eclipse\sdk\platforms\android-18\android.jar" com.myproject.MyActivity
(BTW.: The correct Adroid.jar file is referenced inside the Eclipse project.)
Me also facing the same problem tonight, i found a less tedious way as following...
as "Location" set the javah from your system
as "Working directory" set the project's bin/classes directory (${workspace_loc:/HelloWorld/bin/classes})
as "Argument" set the jni folder as the output directory and point out the class on which you want to run the javah (-d "${workspace_loc:/HelloWorld/jni}" com.example.helloworld.MainActivity)
NB :: Dont forget the double qoute (") around the output in the third step ("${workspace_loc:/HelloWorld/jni}")
A full picture is also attached following
well in case ...
1-> Goto ( cd C:\Program Files\Java\jdk1.7.0_60\bin)
2-> C:\Program Files\Java\jdk1.7.0_60\bin>javah -jni -classpath
F:\Android_OpenGLES\FibonacciNative\bin\classes
-d F:\Android_OpenGLES\FibonacciNative\jni\ com.example.fibonaccinative.FibLib
without creating/using environment variable ...
Following I tried successfully
javah com.sense.kios.Calculation
Note: Include package name.
In case if javah not found as an command and you getting
The program 'javah' can be found in the following packages:
* gcj-4.6-jdk
* gcj-4.7-jdk
* openjdk-7-jdk
* openjdk-6-jdk
Try: sudo apt-get install <selected package>
use direct path of jdk path, in my case it's /usr/lib/jvm/jdk1.8.0/bin/javah.
YES THAT'S CORRECT
more abstract would be
-d ${workspace_loc:/MyProject/jni} -classpath ${env_var:ANDROID_SDK_HOME}\platforms\android-16\android.jar;${workspace_loc:/MyProject/bin/classes} com.myproject.MyActivity

"No classes were specified on the command line" error running javah

I'm on Windows 7, using javah in Cygwin 1.7.13. I'm currently attempting to generate the header file for a native C++ file for my Android project.
Run in the root of the project, the command I'm executing is:
walb#nought /cygdrive/c/Users/walb/stuff/work/workspace/SoundBites
$ javah -verbose -d jni/ -classpath bin/classes uk.co.biogen.SoundBites.activity.SoundBitesActivity
Which results in:
error: cannot access uk.co.biogen.SoundBites.activity.SoundBitesActivity
class file for uk.co.biogen.SoundBites.activity.SoundBitesActivity not found
javadoc: error - Class uk.co.biogen.SoundBites.activity.SoundBitesActivity not found.
[ Search Path: C:\Program Files\Java\jdk1.6.0_22\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\sun
rsasign.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\charsets.j
ar;C:\Program Files\Java\jdk1.6.0_22\jre\classes\bin/classes ]
Error: No classes were specified on the command line. Try -help.
Most of the topics I've read indicate that I've got the -classpath argument correct. The interesting bit of the error message is:
[ Search Path: C:\Program Files\Java\jdk1.6.0_22\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\sun
rsasign.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_22\jre\lib\charsets.j
ar;C:\Program Files\Java\jdk1.6.0_22\jre\classes\bin/classes ]
The last path in that list is C:\Program Files\Java\jdk1.6.0_22\jre\classes\bin/classes, which indicates javah is interpreting the argument completely wrong. The only thing I can think might help is some alternative way of calling javah, but adding its directory to PATH is the only means I can think of.
Just implemented what I wanted in native Android. It's easier to integrate and understand what's going on, at the expense of speed and actually doing what I wanted in the first place.
Does this work (adding a semi-colon before your path for the -classpath argument):
javah -verbose -d jni/ -classpath ;bin/classes uk.co.biogen.SoundBites.activity.SoundBitesActivity

Resources