I just installed cygwin and I am wondering how do I compile and run my java code through cygwin?
My java code is at my desktop saved in a file named Java.
Assuming you have Java SDK for Windows installed.
In the simplest case:
Ensure/Add java to PATH in cygwin:
export PATH=$PATH:"/cygdrive/C/Program\ Files/Java/jdk1.8.0_31/bin/"
(don't forget the backslash after Program)
cd to your desktop:
cd /path/to/Desktop
run java compiler:
javac HelloWorld.java
In complex projects you will need to provide a bunch or arguments to javac to make it compile.
Related
I have been using IntelliJ to run my java programs that require some external jars. I also learned that if I want to compile and run my program from the command line I should do the following:
java -classpath someJar.jar YourMainClass
or for many libraries:
java -classpath someJar.jar;myJar.jar YourMainClass
However, while placed in the src folder where my class it doesn't seem to find my class.
I also like using the Atom text editor but I don't know any package that can import external libraries like an IDE does. So how do I do it in Atom or in cmd in Windows 10? I am kind of a newbie to java dev outisde my beloved IDE, so I would really appreciate some help.
If you're on a Windows system then try compiling (with the jar) using the following command in cmd:
javac -cp .;jar-name.jar *.java
To run the command with the jar use:
java -cp .;jar-name.jar JavaCodeName
If you're on a Unix system then you can try the following to compile in terminal:
javac -cp jar-name.jar:. *.java
And to run it use:
java -cp jar-name.jar:. JavaCodeName
I'm not too familiar with Atom so I don't know if there is an attachment to do this, but it should work for terminal / command prompt.
I am trying to compile some files using cygwin shell.
java file that I am trying to compile is C:\Users\Programs\x.java
For windows command prompt
C:\Users\Programs>javac x.java
No errors.
For Cygwin shell
$ javac /cygdrive/c/Users/Programs/x.java
Error:
javac: file not found: \cygdrive\c\Users\Programs\x.java
Why do I get an error in cygwin shell though it runs in windows command prompt
javac is Windows application. It has no clue about /cygdrive. Always keep it in mind and pass valid Windows paths.
So use javac 'c:/Users/Programs/x.java' or javac c\:/Users/Programs/x.java
Also, as in most Unix shells \ must be escaped if you need to use it.
I think that the problem is that "cygdrive" is a pseudo-device that is implemented in a Cygwin-specific library. If the executable you are running has not been linked against that library (and the Oracle Java executables have not!) it won't correctly resolve the "/cygdrive" path component.
I am working on an embedded system with openwrt root files system and linux kernel.
I have compiled the trunk, no problem with that. I have installed the Java resources in /usr/bin, /usr/lib and /usr/share, but I haven't been able to compile some simple programs that I have done in Eclipse. I have used javac to compile a hello world and I obtain the .class file but when I try to execute the helloworld.class file in my embedded system with:
java helloworld.class
it does nothing, it just says:
/usr/bin/java: line 1: syntax error: unexpected word (expecting ")")
When I execute this in my computer it runs, so I guess it is because I have to cross-compile the java files, so how can I do that?
The problem is not with your class, but with a syntax error in the /usr/bin/java script - try cat /usr/bin/java
Just try the java command withoud .class extension like
java helloworld
I have a homework assignment in Java that is tested using the commands:
make
./<program_name> <arguments>
my make file compiles my java program successfully, but how can the program be run without using the command:
java <program_name>
I have investigated how to convert a .jar into an .exe but I am convinced that is not the answer I am looking for.
I believe the test is run on a Linux machine. Is there something I can include in the make file to cause the command
./<program_name>
to run a compiled java class?
Without converting the java program in a native executable file, that will be different for linux, for windows and any other platform (so you will loose Java portability), the only thing you can do is to create a launch script.
On *nix system you can create a bash script and on windows a batch script. Then in this script you have to call java <program_name>.
With the script you are now able to launch your application with a single command.
For example on unix you can create myapp.sh:
#!/bin/bash
java -classpath bin com.test.YourApp $*
and make this script runnable with command
chmod a+x myapp.sh
in this example when you write myapp.sh command you launch your Java class com.test.YourApp using the folder bin as classpath.
I have a folder on my desktop titled "Stuff" and in that folder I have the following:
Hello.java
mail.jar
And Hello.java imports from mail.jar, so I need to tell Hello.java to look for mail.jar.
From a Windows command line and from a unix command line, how can I compile this and run this?
Compile:
javac -cp .;mail.jar Hello.java
where ; is for Windows; use : for *nix.
and run:
java -cp .;mail.jar Hello
where again, use ; for Windows and : for *nix.
-cp tells both javac and java what classpath to use, and as your files are in the local directory where you're executing the command, you can use . for the Hello part and the name of the jar for the paths inside the jar. Wikipedia has a decent article on classpaths.
Mind you, if you're going to be doing this on a regular basis, you may want to set your CLASSPATH environment variable rather than constantly using the -cp flag. Both java and javac use the CLASSPATH variable.
For my own development machine, I actually include . in my CLASSPATH variable, for convenience. It's not something I would do on a production or build/test box, but it's very handy for development purposes. You'd want to have your usual jars in it as well.
Assuming Hello.java does not contain a package declaration, on Windows:
javac -cp mail.jar Hello.java
java -cp mail.jar;. Hello
The only difference on Unix platforms is that you separate the elements of the classpath with a scolon instead of a semicolon:
java -cp mail.jar:. Hello
Follow this tutorial and you should be able to do it in no time:
Java Compilation
You also shouldn't have any problems with the classpath because your classes are in the same folder