all am working a project for compiling and running a java source code via my application.
here am stuck will explain my environment here..
java 1.6.0_35 32bit and eclipse indigo using operating system windows 64 bit
when i used this code
String command = "javac "+file.getAbsolutePath();
System.out.println(command);
Process p = Runtime.getRuntime().exec(command);`
got this exception ..think error show file doesnt exist but i checked it,which is here
and when i execute via command line which compiles,but the eclipse shows this error
java.io.IOException: Cannot run program "javac": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)`
can anyone fix this ,i tried a lot ..
You don't need to use the command line, you can access javac programmatically:
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
javac.run(null, null, null, arguments);
See
The Java 6.0 Compiler API
and http://openjdk.java.net/groups/compiler/guide/compilerAPI.html for examples
It is portable (works on other systems) and will avoid problems with paths and command line.
You can use Java 6 Compiler API . Also this link might be useful..
Your problem seems not so much that the file you want to compile is not found, but that the javac command itself is not found. Make sure that the Runtime can find the javac executable, eventually by passing the "PATH=/path/to/java/dir" to exec.
See: Runtime.exec(command, enviroment, dir)
Related
I have build my project using OpenCV. When I run my project through netbeans it runs fine.. but when I create its jar file via Clean and Build it doesn't open.
it displays a message in output screen when it is build that is..
To run this application from the command line without Ant, try:
java -jar "C:\Users\Rafi Abro\Documents\NetBeansProjects\WebCam\dist\WebCam.jar"
I also tried to open my project through this command..
java -jar WebCam.jar
but it displayed error below:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java2410 in
java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at javaanpr.Main.main(Main.java:154)
please can anyone help me through out this problem..
Thanks
This is because you have included VM option path in netbeans. and Netbeans Automatically calls it whenever it is executed.
now when you are trying to run command. i.e.
java -jar WebCam.jar
there is not path defined in command, thatsy you are getting that error..
try this command:
java -Djava.library.path="C:\OpenCV\opencv\build\java\x86" -jar WebCam.jar
hope this will help you.!
compiled my project in netbeans IDE and when I run the jar nothing happens just gives me a error occured while trying to run it. I think I found the problem just not sure how to fix it. Help please!
This is what happens when I check the jar in CMD
C:\Users\Mac\Desktop>java -jar dist\pong.jar
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr
ary.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:73)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:95)
at org.lwjgl.Sys.<clinit>(Sys.java:112)
at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
at mw.Main.initDisplay(Main.java:36)
at mw.Main.main(Main.java:25)
C:\Users\Mac\Desktop>
When I check the manifest file it's pretty much empty:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
when I check the jar in CMD C:\Users\Mac\Desktop>java -jar dist\pong.jar
You need to use the -cp switch while running the jar using the java command.
Documentation
You can easily google to find more examples on how does -cp works.
You need to create a library and add a reference to it in your project
See Setting Up LWJGL with Netbeans
At a guess I'd say you are missing step 5 from Setting Up a Project to Use LWJGL in NetBeans
Finally, you need to tell NetBeans where the Native Libraries (i.e, DLL, JNILIB, DYLIB, SO files) are for your system so that the natives are linked when running.
Select the Run category and then type the following into the VM Options, replacing what is in bold to suit your system:
-Djava.library.path=<lwjgl-X.X path>/native/<linux|macosx|solaris|windows>
Update: Didn't realise you were doing it from the command prompt. You say you are running
java -jar dist\pong.jar
You need to add the -D option so something like this:
java -Djava.library.path=<lwjgl-X.X path>/native/windows -jar dist\pong.jar
So I've been Googling and searching for literally 3 hours now for this solution. I used to have some files for this purpose but they have been deleted over the years.
Basically, I have a .jar file named RiD.jar located in: /Users/Mom/Desktop/RiD
The .jar was recently updated with changes that may allow the possibility of support for Mac OS X. I have been given the task of testing it in a Mac OS X environment.
I need to make a double-clickable file that will open the .jar file that can be added to the initial download (a .rar file containing a folder of all the included files, one of which being the RiD.jar) that will work universally for all Mac users.
Whether it uses AppleScript or something else, as long as it works I'm happy.
For the creation of any specific file type in your recommendation, please do me the favor of explaining what program would be used to make this file type and any other relevant information, as my OS X programming knowledge is limited.
Any help is appreciated and any questions you might have for me are more than welcome.
EDIT:
Here's a copy of what I get when I try and open it via the Terminal:
I have the newest version of Java included in the Mac App Store (e.g. Java 6 update 41).
java.io.IOException: Cannot run program "cmd": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at Run.main(Run.java)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
at java.lang.ProcessImpl.start(ProcessImpl.java:91)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 4 more
EDIT: I just updated to Java 7 update 15 from the Oracle website because it is needed to run a Java applet in a browser that must work for the purpose of the original RiD.jar file. I still get the same error as from Java 6 update 41.
EDIT: I've spoken with the developer and he's removed the use of cmd when using OS X. I now have a slightly different error:
My-Computer:~ Mom$ cd /Users/Mom/Desktop/RiD
My-Computer:RiD Mom$ java -jar RiD.jar
java.io.IOException: Cannot run program "javaw": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at Run.main(Run.java)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
at java.lang.ProcessImpl.start(ProcessImpl.java:91)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 4 more
enter code here
The error trace seems to show that the Java code calls "cmd", presumably to run some script. If this is the case, it will obviously not work on a Mac without some code change of the underlying Java code.
In general, to run JAR files on Mac requires that you install Java from Oracle. After that install, jar files should be associated to launch with Java on a normal double click.
UPDATE (to match updated question): The JAR file is trying to call the javaw, which is the console-less runtime for Java. Without seeing the code, I can't say why it's doing this, but it looks like a PATH error. In the terminal, try to write
which javaw
My guess is that it won't find the correct destination for javaw. The solution is either that you manuaylly edit your environment to include a path to javaw, if you can locate it, otherwise, easiest is to re-install Java from Oracle and see if the problem disappears.
It could also be that the script in the JAR file assumes an incorrect path for javaw, for example by assuming it to be located in a typical Windows path.
I've been trying to execute the commands to run the RMI Hello world example but I Failed!
My execution steps are taken from here: http://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html
The commands are:
javac: works fine and I get the class files
rmiregistry &: I get something like [1] 17122
java -Djava.rmi.server.codebase=file:/users/ha/RMI/ example.hello.Server:
Gives me an error message
Exception in thread "main" java.lang.NoClassDefFoundError:
example/hello/Server Caused by: java.lang.ClassNotFoundException:
example.hello.Server at
java.net.URLClassLoader$1.run(URLClassLoader.java:202) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:190) at
java.lang.ClassLoader.loadClass(ClassLoader.java:306) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at
java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I did solve this problem by running each command on a new terminal window.
First I started with javac for the .java files, then run the command rmiregistry &.
Second I opened a new terminal window and run the command java for the Server class.
Third I opened a new terminal window and run the command java for the Client class.
and it works with me!
On the Mac, the Users directory in the root directory is written with a capital letter.
You wrote:
file:/users/ha/RMI/
file:/Users/ha/RMI/ would be correct. This applies only to file systems that have been setup explicitly to setup to ignore case (Case-insensitive). At least that is what the comments below are saying. I cannot verify this myself.
A classpath is also needed under many circumstances. It depends on your dependencies. But if you needed the classpath in windows, you will need a similar one on Mac.
Depending on where you .class files are, I would add the classpath to your invocation.
java -Djava.rmi.server.codebase=file:/users/ha/RMI/ -cp=/Users/ha/RMI example.hello.Server
This assumes, of course, that the class files are under /Users/ha/RMI/example/hello
Just look inside of the directory to verify that the class files are there. It should be the directory that you designated after the javac command and -d argument.
I used three terminal windows for execution of a rmiregistry program.
1.Use this terminal window to compile all your files (javac filename.java) and use the command "rmiregistry &" this is the windows equivalent command for "start rmiregistry". (Note:'start' command doesn't work on the macOS)
2.Use this to run the java file ie. Server file (ie. java RMI_Server)
3.Use this to run the client file ie. Client file (ie. java RMI_Client 127.0.0.1)
And yeah it worked for me. (Note: The Directory should be set properly in the terminal)
Im quite annoyed at having to ask this but I cant get it to work. Currently I have a project with:
5 Classes in the src/ folder
2 JARS named profiles.jar and
classifier.jar in the root folder
I want to create a "makefile?" or "batch file?" to compile and run these classes FROM THE WINDOWS COMMAND LINE, but first add the jars to the buildpath? Im not sure how I go about this
When I try to do it, it says that the class is not found, most likely due to me not adding the jars to the buildpath correctly. What are the commands I need to use to run this in command prompt?
Thanks
Philip
EDIT
Thanks for the help, Im having alot of trouble getting it to work tho
Currently I have a project with 5 classes in the src folder, and 2 jars in the jar folder
Here are the commands
Im running:
set
CLASSPATH=C:\wamp\www\news\UserProfiling\jars\classifier.jar
;C:\wamp\www\news\UserProfiling\jars\profiles.jar
Then from the root folder, Im running:
javac src/*.java
Then:
java -cp ./src:./jars/*
src/Interaction
Interaction is the main class, Im getting all sorts of noclassfound errors, am I doing something wrong?
Many thanks Philip
THE ERROR
java -cp ./src:./jars/* Interaction
Exception in thread "main"
java.lang.NoClassDefFoundError:
Interaction Caused by:
java.lang.ClassNotFoundException:
Interaction
at java.net.URLClassLoader$1.run(Unknown
Source)
at java.security.AccessController.doPrivileged(Native
Method)
at java.net.URLClassLoader.findClass(Unknown
Source)
at java.lang.ClassLoader.loadClass(Unknown
Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown
Source)
at java.lang.ClassLoader.loadClass(Unknown
Source)
at java.lang.ClassLoader.loadClassInternal(Unknown
Source) Could not find the main class:
Interaction. Program will exit.
In version older than or equal to Java version 5 you must specify each jar individually, and the root of your source, on your classpath e.g.
java -cp a.jar:b.jar:c.jar:./src MainClass
In version 6 you can use wildcards for the jars e.g.
java -cp ./src:* MainClass
but it might be cleaner putting your jars into a sub directory e.g.
java -cp ./src:./jars/* MainClass
So basically, your makefile or start script needs to construct a command like one of the above.
More info - Sun docs (v6)
Update - in response to your second edit, you need to specify the full main class name, so if the class is in a package called 'com.mypackage.MainClass' then you need to do:
java -cp ./src:./jars/* com.mypackage.MainClass
I'd also suggest getting the command working as a standalone command first, before getting the whole script running. By removing moving parts it will be faster to debug and easier to see what's going on.
I would suggest you take a look at ant or maven. Ant is a solution to do pretty much straightforward what you want to do, maven is not as straightforward but has its advantages when it comes to managing dependencies.
About your second question : if you use
java -cp ./src:./jars/* src/Interaction
it will try to launch the class src/Interaction, which does not exists. src is already in your classpath, so you just have to do
java -cp ./src:./jars/* Interaction
Found your problem, I think.
javac src/*.java
java -cp ./src:./jars/* src/Interaction
This problem plagues many beginners: You need to run javac and java from the directory where your source tree starts. In your case, that directory is src. Since src does not appear in your package names, it should also not appear in your compile/execution paths.
So you should
cd UserProfiling/src
and run
javac *.java
and
java -cp .:../jars Interaction
from there.