Old Linux Admin 2008 -->
Home Debian Install + WIFI WIFI Install cont'd Debian Install Done! Cisco Soho97 Config LinuxCmds Firewalls AntiVirusInfo.html Shorewall Firewall Old Linux Admin Series Java RMI Asterisk_PBX_Info Databases - A Research Paper VPN PPTPD Info Aloha Packet Radio Transmission Apache2_Info.html BasicSecurityConcepts.html C_and_Java_Info.html CV.html Fetchmail_SSL_Info.html Hard_Drive_Info.html LVM2_Volume_Info.html MYSQL_Info.html Networking_Info.html Packet_Inspection_Info.html Security_Info_Wireshark_WIFI.html Snort_Info.html Subnet_Mask_Info.html Useful_One_Liners.html New DebianAdmin Site

New document on Netbeans IDE and Java Remote Method Invocation (RMI) here: JavaRMI

It is possible to compile and run C and Java code and programs from the command line in Linux, as you would expect from an OS written in C.

This is very convenient, as it negates the requirement of a dedicated compiler or other environment, as in Windows, using 3rd party software such as Borland, Blue J etc. You can also run Java progs on the Cmd Line in Windows by installing the Sun JDK and CD-ing to its install bin directory and running the javac.exe to compile, and the java.exe to run the .class file once compiled.

 

A command line compiler in Linux is gcc, so check this is installed,

# gcc

gcc: no input files

 

If not present, installing module-assistant usually installs all compiler and source associated requirements for C program and package builds, including gcc, cpp and g++

#apt-get install module-assistant

 

For Java, there are many Development Kit (JDKs) and Runtime environments (JREs) as can be seen with a wide search of what's available:

# apt-get install jdk*

Reading package lists... Done

Building dependency tree

Reading state information... Done

Note, selecting libkjdsp-java for regex jdk*

Note, selecting openjdk-6-demo for regex jdk*

Note, selecting jdresolve for regex jdk*

Note, selecting jd for regex jdk*

Note, selecting libjdom1-java for regex jdk*

Note, selecting libpostgis-jdbc for regex jdk*

Note, selecting gjdoc-native for regex jdk*

Note, selecting openjdk-6-dbg for regex jdk*

.......blah......

 

Research your needs if you are a programmer, or go trial and error.

All that is required for java compilation on the cmd line, is:

# javac

and

# java

along with the necessary libraries, that will come with the correct JDK choice made above. I found the openjdk-6-source worked for me for a previously written java program, primes_findpubKey.java

which is a slightly modified version of the C programme below.

 

A simple C programme that prints Prime Numbers example to start, with the files in the root directory /:

 

The code is in a text file called prime.c

# cat /prime.c

// prime.c

 

#include <stdio.h>

#include <math.h>

int main()

{

int i,j;

int a;

int input;

printf("Input prime limit eg < 1000..");

scanf("%d", &input);

for( i=2; i<input; i=i+1 )

{

a=0;

for(j=2; j<i; j=j+1)

if (i%j == 0)

a=1;

if (a == 0)

printf("%d ", i);

 

}

return 0;

}

 

To compile this:

# gcc -v /prime.c -o /print-primes

where the -o switch names the compiled executable /print_primes

There is no output without the verbose option, and a lot with it:

 

Using built-in specs.

Target: i486-linux-gnu

Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-cld --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu

Thread model: posix....blah...

 

If you list the / dir, you should see a bright green colourised file print_primes indicating its executable status. You can run this as is:

 

# print_primes

Input prime limit eg < 1000..

999

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

 

The Java version of this program is:

# cat primes_findpubKey.java

 

import java.util.Scanner;

public class primes_findpubKey

{

public static void main ( String args[ ] )

{

Scanner stdin = new Scanner ( System.in) ;

int i ;

int j ;

int a ;

int k = 0 ;

//int pubKey ;

int logic ;

// int limit;

System.out.println("Input prime upper limit eg < 1000..");

int limit = stdin.nextInt();

System.out.println("Input pubKey...");

int pubKey = stdin.nextInt();

for( i = 2 ; i <= limit ; i = i + 1 )

{

a=0;

for( j = 2 ; j < i ; j = j+1 )

if ( i % j == 0)

a=1;

if (a == 0)

System.out.print( "," + i );

System.out.print("") ;

if ( pubKey % i == 0)

//k = 1 ;

System.out.println( "A Private KEY IS : " + i );

}

}

}

 

This program was written to help find the only 2 the prime divisors of a larger prime number, an aspect of the principle involved in SSL and Internet Key Exchange (IKE) processes on the Web.

The idea to find them, was to take the known large prime you need to find the divisors for, and square root it, find the closest prime number to this and see if it divides into the larger prime by a prime number amount.

This programme prints the prime numbers up to the size of the large prime as they are found, and divides them into the large prime until numbers with no remainder are found.

 

Two known large primes to play with are 50693 and 1506181.

 

To compile primes_findpubKey.java

 

# javac /primes_findpubKey.java

 

This generates the file /primes_findpubKey.class

 

To run the .class file, use java with the class extension dropped:

 

# java primes_findpubKey (note no fwd slash!)

 

Input prime upper limit eg < 1000..

225

Input pubKey...

50963

,2,3,5,7,11

A Private KEY IS : 11

,13,17,19,23,29,31,37,41

A Private KEY IS : 41

,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113

A Private KEY IS : 113

127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223

 

As the SQRT of 50963 is 225.7ish, we need only enter the rounded number here to find some primes up to this number, as the two Primes above and below the SQRT are 223 and 227 which when multiplied are 50621 so less than 50963 so we are looking for a higher and probably lower number for a pair again.

One number at least may be in the range less than the SQRT of 225, i.e 113.

The 2nd divisor is found by diving 50963 by 113 to get 451.

For the other example, 1506181, the SQRT is 1127.26-ish, so put in 1127 for the range, and 1506181 for the PubKey:

java primes_findpubKey

Input prime upper limit eg < 1000..

1127

Input pubKey...

1506181

,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097

A Private KEY IS : 1097

,1103,1109,1117,1123

 

Divide 1506181/1097 to find the other divisor = 1373

The prog verifies this by checking, with 1374 as the range:

# java primes_findpubKey

Input prime upper limit eg < 1000..

1374

Input pubKey...

1506181

,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097

A Private KEY IS : 1097

,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373

A Private KEY IS : 1373

 

Steve 2008-2022