So I'm new to java (more familiar with python) so sorry if this is a really silly question. I have a folder of java files. I created a new directory and compiled the java files into it.
~ mkdir classes
~ javac -d classes my_code/*.java
Now, after initially compiling the code, I was able to run the scripts in terminal using:
java classes/some_script
Next, to make updates in the code I'm using a IDE (intellij). But after I save the some_script.java file the updates dont show up when I rerun the statement above in terminal.
Is there something I have to do (in terminal) to update the files so that when I rerun the statement in terminal the changes I made in the IDE show up? (ps I'm using a mac).
Thanks in advance.
Each time you modify the source code you need to compile the code. Contrary to python you cannot just save the file and run it.
Related
I've been looking around for days trying to find a way to compile a whole directory that doesn't only contain Java classes. I've got no idea how to add text files and dependency to the compiling process. Does someone know a way to do that? I know JavaCompiler is a thing but I can't get it to do that. I'd really appreciate it if anyone could send me some sample code using anything able to do what I want.
If you just want to compile the java files in a directory/folder, you can open a terminal , go to the specific folder and run
javac *.java
If you want to do this from your java application, you can use the Runtime class to execute operating system commands from your java program.
For example you can create a bash script or .bat file, put there all your commands, and then call
Runtime run = Runtime.getRuntime();
Process process=runtime.exec("Compile.bat");
You could also create a pom.xml, and use maven to build your project (by calling maven from you application)
I'm currently learning java from a book and I just reached packages. I've been saving all my files on my desktop and compiling/running programs from Mac's Terminal console.
John-MacBook-Pro:~ john$ cd desktop
John-MacBook-Pro:desktop john$ javac Learning.java.
John-MacBook-Pro:desktop john$ java Learning
.... program executes and so on .....
Now I save my .java files into a package (create a new folder). Let's call the package 'book' And I'm told to run programs like this now:
javac book/Learning.java
java book.Learning
This works when I have one folder, sure, but when subclasses and more packages are added into that book folder how do I compile things deeper in? Not to mention how to run them afterwards?
The book might have assume prior knowledge so it just dives right in and tells me to setup CLASSPATH or use -classpath on my Macbook before attempting. I've tried various commands on terminal and it seems to compile sometimes where I have to manually change directory to open each folder (which is a lot of typed commands). Trying to run any classes always result in class not found. Every other answer seems to have some of the basic stuff setup already or is explained in terminology I don't understand yet.
When more classes are added, you compile them all:
javac book/Learning.java book/chapter/Chapter.java ...
You run the main class exactly the same way:
java book.Learning
If you're not in the package where the root of the package tree is (i.e. your desktop directory), you pass it in the classpath:
java -classpath /users/Leosam/desktop book.Learning
Note that it works on macOS the same way as on any other platform.
I have tried posting this request in Cloud9's forums, but since it never posts, I am hoping someone here could give me a hand. (The forum keeps saying my question is being submitted for review, but then never shows up).
I can't get Java code to compile and run. I've Googled the issue and read other posts in their limited forums, but I can't get it to compile and run a simple Hello World! program. My .java filename matches the class and the code is fine.
I have tried putting my .java files in the following directories:
test/java/
test/src/
I just get this:
bash: src/HelloWorld.java: No such file or directory
I tried this: http://www.programmingforliving.com/2014/09/java-coding-with-cloud9-ide.html
But I just get:
Running HelloWorld.java
Error: Could not find or load main class HelloWorld
Any suggestions? Given I'm essentially a Java noob I am hoping the solution is fairly simple.
Without you pasting in your code it's tough to tell but make sure your class and filename are both titled HelloWorld. Make sure your src folder is in your main workspace folder so your file should be at /home/ubuntu/workspace/src/HelloWorld.java.
Also, this documentation looks pretty similar to the blog you referenced but still worth a look https://docs.c9.io/docs/custom-runners.
I think that Codenvy is much better in java than Cloud9. It provides easy build & run configuration, code completion, project sharing with permissions or factory (creates clone of your project with one click in minutes).
Take a look at official java spring factory. This link opens temporary(personal) java project ready to code, build & run.
Also Codenvy has project wizard that has a lot of project types including java console app (uses maven).
Ok, decided to screw around with it and figured it out... (then saw that Brady Dowling had posted a link that explained in better detail what needed to be changed).
At any rate, I noticed a few things in Cloud9.
Even after making the changes and saving, it would still not compile correctly. I had to close my session of C9 and go back in. Then it would work.
After compiling, and trying to run the program, it wasn't automatically picking the Java runner I made. So if you are running into issues, make sure the Java (or whatever you decided to name it) runner is selected in the console.
You have to compile and run separately. Kind of annoying, but at least it works. Unlike C/C++ in C9, it won't compile and run by simply hitting 'Run'. If you do, it creates an error saying the .class doesn't exist. Simple as pressing F7 to build, then F5 to run.
Anyway, I took screenshots of what I had to change to get it working. Essentially, just the directories it looks for the .java file in (the right arrows) and the directory it looks for/compiles the .class file to (the left arrows). Since I was putting the .java and .class in the same directory (java), I just changed both to 'java'. In the links Brady and myself posted, they used .bin (for .class) and src for (.java).
1st picture is the Builder file (I called it javac) and the 2nd picture is the Runner file (I called it Java).
Anyway, I hope this helps someone out. I am really liking C9 and it perfectly suits my novice needs at the moment. I like the ability to dabble in multiple languages to see what I like without having to switch applications. The real-time live preview HTML/CSS like Brackets is another huge plus for me.
Regards
For what I understand, C9 workspaces don't come with jdk installed. But I always use these commands:
sudo apt-get update
(wait a while for it to finish processing. Then)
sudo apt-get install default-jdk
(Then It will ask you for some permission. Type "Y")
And then it compiles and runs normally using the terminal.
Sometimes you will also need to install a package to get javac.
Example:
sudo apt-get install openjdk-7-jdk
The java command works right of the box. You may java -version to see that it is Ubuntu running. You can also locate the java binary and see that it is amd64. That is all you need to know to download jdk manually from Oracle.com, like
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u92-b14/jdk-8u92-linux-i586.tar.gz
tar xf jdk-8u92-linux-i586.tar.gz
and voila, jdk-1.8.0_92/bin/java(c) can be executed! It allows you to compile java files.
I use SPEC as a tool to measure java files running time.
I'm using SPEC2008 which can be downloaded here.
I installed spec2008 (java -jar SPECjvm2008_1_01_setup.jar) and ran it (java -jar SPECjvm2008.jar as written in the README file).
I got a results file and a report that everything completed successfully.
Now i need to examine how some changes affect SPEC's running time.
I made the changes in some of SPEC'S files (the java files inside the following folders: compiler,compress,crypto,derby).
Now i can't run it again using java -jar SPECjvm2008.jar because the changes i made include jomp commands (\\omp parallel for and such). [JOMP is OpenMP for JAVA]
To compile files with such commands i used to do the following:
First, download JOMP. Then:
change .java extension to .jomp . Then type:
java jomp.compiler.Jomp MyFile
(now i got a .java file)
compile to .class:
javac -classpath YourJompDownloadPath\jomp1.0b.jar; MyFile.java
java -Djomp.threads=n MyPackage.MyFile
So i can't do this now because:
There are too many files and it would take time changing all extensions to .jomp
There are too many files and i need to run all of them as a whole (just as SPEC does), but in JOMP commands i only know how to run each file independently.
If there's a workaround for #2 then i'll do #1 manually. So i'm especially looking for a solution/workaround for #2.
edit:
OK so i made a .BAT file only for step 1 (see method here) because it seems that SPEC has only .java files so i'll provide it with .java files only. For the 2nd step i think i need to just run SPEC (java -jar SPECjvm2008.jar). For the 3rd step, i think instead of normally running SPEC, i need to run it with the parameters in step 3 (Djomp.threads). That is:
java -Djomp.threads=n -jar SPECjvm2008.jar
Now there's a new problem. I get the warning:
Recompile with -Xlint:unchecked for details
Which was addressed here. But in that link it's run with javac and not java. javac doesn't recognize the parameter Djomp.threads (and java doesn't recognize Xlint). So i think i'm looking for a way to run a .jar file while using Djomp.threads and Xlint.
Note: running the .jar file SPECjvm2008.jar eventually runs the java files i need. That's why i said "i'm looking for a way to run a .jar file..."
so, I've been doing some searching, and i can find somethings on how to run an external application, but i cant get them to work! I've been working on this for a while, and its really annoying.
what i want to do is run a .jar file in the directory
C:\Program Files\AVTECH\NPS\Files\bin\NPS.jar
and I've tried a bunch of different things with the code
Process p = Runtime.getRuntime().exec("dir goes here");.
also
Process p = Runtime.getRuntime().exec("C:\\Program Files\\AVTECH\NPS\\Files\\bin\\NPS.jar");.
if i'm correct, it uses command prompt to do this? or at least the MS-DOS language. i did some of that kind of thing a few years ago, but i don't remember how one would do this... I've never worked with this kind of thing in java before...
could someone help please? thanks in advance.
Runtime.exec() is working just like if you were typing a command.
Launching a jar file is not working : you have to invoke
java -jar /path/to/my/jar
Check Oracle's documentation on how to execute a jar file.
The actual command should be java -jar C:\\Program Files\\AVTECH\NPS\\Files\\bin\\NPS.jar. I mean -- if the jar file is indeed executable, this doesn't mean it will run, by just trying to invoke it. You need to tell Java to run it as shown above.
In addition, MS-DOS is not a language -- it stands for Microsoft Disk Operating System. Nowadays, you have this as a Command-line Prompt (Shell) built into Windows.
You need to run the command as a call to the executable and a set of arguments. Check this version of Runtime.exec(String[] cmdarray). If need be, there's also a version of Runtime.exec() that takes a base directory in which to start the executable.